diff --git a/samples/ControlCatalog/Pages/ComboBoxPage.xaml b/samples/ControlCatalog/Pages/ComboBoxPage.xaml
index eca2c4762c..ab347ac30c 100644
--- a/samples/ControlCatalog/Pages/ComboBoxPage.xaml
+++ b/samples/ControlCatalog/Pages/ComboBoxPage.xaml
@@ -12,8 +12,8 @@
+ WrapSelection
Inline Item 4
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -114,9 +136,6 @@
-
- WrapSelection
-
diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs
index 78bb191e93..f1cc84f7a4 100644
--- a/src/Avalonia.Controls/ComboBox.cs
+++ b/src/Avalonia.Controls/ComboBox.cs
@@ -5,10 +5,12 @@ using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
+using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
+using Avalonia.Metadata;
using Avalonia.Reactive;
using Avalonia.VisualTree;
@@ -71,6 +73,23 @@ namespace Avalonia.Controls
///
public static readonly StyledProperty VerticalContentAlignmentProperty =
ContentControl.VerticalContentAlignmentProperty.AddOwner();
+
+ ///
+ /// Defines the property.
+ ///
+ public static readonly StyledProperty SelectionBoxItemTemplateProperty =
+ AvaloniaProperty.Register(
+ nameof(SelectionBoxItemTemplate), defaultBindingMode: BindingMode.TwoWay, coerce: CoerceSelectionBoxItemTemplate);
+
+ private static IDataTemplate? CoerceSelectionBoxItemTemplate(AvaloniaObject obj, IDataTemplate? template)
+ {
+ if (template is not null) return template;
+ if(obj is ComboBox comboBox && template is null)
+ {
+ return comboBox.ItemTemplate;
+ }
+ return template;
+ }
private Popup? _popup;
private object? _selectionBoxItem;
@@ -159,6 +178,16 @@ namespace Avalonia.Controls
set => SetValue(VerticalContentAlignmentProperty, value);
}
+ ///
+ /// Gets or sets the DataTemplate used to display the selected item. This has a higher priority than if set.
+ ///
+ [InheritDataTypeFromItems(nameof(ItemsSource))]
+ public IDataTemplate? SelectionBoxItemTemplate
+ {
+ get => GetValue(SelectionBoxItemTemplateProperty);
+ set => SetValue(SelectionBoxItemTemplateProperty, value);
+ }
+
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
@@ -322,7 +351,10 @@ namespace Avalonia.Controls
{
PseudoClasses.Set(pcDropdownOpen, change.GetNewValue());
}
-
+ else if (change.Property == ItemTemplateProperty)
+ {
+ CoerceValue(SelectionBoxItemTemplateProperty);
+ }
base.OnPropertyChanged(change);
}
@@ -433,7 +465,7 @@ namespace Avalonia.Controls
}
else
{
- if(ItemTemplate is null && DisplayMemberBinding is { } binding)
+ if(ItemTemplate is null && SelectionBoxItemTemplate is null && DisplayMemberBinding is { } binding)
{
var template = new FuncDataTemplate