From fb202aa060fc5ae1b392e7618ab51fd45853d229 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 4 May 2023 17:08:47 +0800 Subject: [PATCH 1/2] feat: respect DisplayMemberPath in SelectionBoxItem. --- src/Avalonia.Controls/ComboBox.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 1234b66383..dadda4e0ec 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -443,7 +443,22 @@ namespace Avalonia.Controls } else { - SelectionBoxItem = item; + if(ItemTemplate is null && DisplayMemberBinding is { } binding) + { + var template = new FuncDataTemplate((_, _) => + new TextBlock + { + [TextBlock.DataContextProperty] = item, + [!TextBlock.TextProperty] = binding, + }); + var text = template.Build(item); + SelectionBoxItem = text; + } + else + { + SelectionBoxItem = item; + } + } } From 6b56d440229609cf88cb7742691d5a017af7dbec Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 4 May 2023 17:11:47 +0800 Subject: [PATCH 2/2] feat: add combobox samples. --- samples/ControlCatalog/Pages/ComboBoxPage.xaml | 15 +++++++++++++++ .../ViewModels/ComboBoxPageViewModel.cs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/samples/ControlCatalog/Pages/ComboBoxPage.xaml b/samples/ControlCatalog/Pages/ComboBoxPage.xaml index 748a46c447..f3f6cfe0af 100644 --- a/samples/ControlCatalog/Pages/ComboBoxPage.xaml +++ b/samples/ControlCatalog/Pages/ComboBoxPage.xaml @@ -98,6 +98,21 @@ Inline Item 3 Inline Item 4 + + + + + + + + + + + + + + + WrapSelection diff --git a/samples/ControlCatalog/ViewModels/ComboBoxPageViewModel.cs b/samples/ControlCatalog/ViewModels/ComboBoxPageViewModel.cs index d3e4ea7c31..6ab7bb02e3 100644 --- a/samples/ControlCatalog/ViewModels/ComboBoxPageViewModel.cs +++ b/samples/ControlCatalog/ViewModels/ComboBoxPageViewModel.cs @@ -16,5 +16,20 @@ namespace ControlCatalog.ViewModels get => _wrapSelection; set => this.RaiseAndSetIfChanged(ref _wrapSelection, value); } + + public ObservableCollection Values { get; set; } = new ObservableCollection + { + new IdAndName(){ Id = "Id 1", Name = "Name 1" }, + new IdAndName(){ Id = "Id 2", Name = "Name 2" }, + new IdAndName(){ Id = "Id 3", Name = "Name 3" }, + new IdAndName(){ Id = "Id 4", Name = "Name 4" }, + new IdAndName(){ Id = "Id 5", Name = "Name 5" }, + }; + } + + public class IdAndName + { + public string Id { get; set; } + public string Name { get; set; } } }