From 5d8c4b199d46ded59f7cdd7ff1ae27a5d742b1a0 Mon Sep 17 00:00:00 2001 From: StulittleLi Date: Mon, 2 Feb 2026 08:45:03 +0800 Subject: [PATCH] Make the SelectionBoxItem built with ItemTemplate --- src/Avalonia.Controls/ComboBox.cs | 52 +++++++++++++++++-------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index c4af8467c0..5fa6ffc38f 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using Avalonia.Automation.Peers; using Avalonia.Controls.Metadata; +using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Shapes; using Avalonia.Controls.Templates; @@ -534,18 +535,36 @@ namespace Avalonia.Controls private void UpdateSelectionBoxItem(object? item) { - var contentControl = item as IContentControl; - - if (contentControl != null) + var target = item; + IDataTemplate? itemTemplate=null; + if (SelectionBoxItemTemplate is { } selectionBoxItemTemplate) + { + itemTemplate = selectionBoxItemTemplate; + }else if(ItemTemplate is {} itemTemplateFromItems) + { + itemTemplate = itemTemplateFromItems; + } + else if(DisplayMemberBinding is { } displayMemberBinding) + { + itemTemplate=new FuncDataTemplate((_, _) => + new TextBlock + { + [TextBlock.DataContextProperty] = target, + [!TextBlock.TextProperty] = displayMemberBinding, + }); + } + if(itemTemplate != null&&itemTemplate.Match(target)) + { + item=itemTemplate.Build(item); + } + if (item is IContentControl contentControl) { item = contentControl.Content; } - var control = item as Control; - - if (control != null) + if (item is Control control) { - if (VisualRoot is object) + if (VisualRoot is not null) { control.Measure(Size.Infinity); @@ -566,22 +585,7 @@ namespace Avalonia.Controls } else { - if (item is not null && ItemTemplate is null && SelectionBoxItemTemplate 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; - } - + SelectionBoxItem = item; } } @@ -589,7 +593,7 @@ namespace Avalonia.Controls { if (SelectionBoxItem is Rectangle rectangle) { - if ((rectangle.Fill as VisualBrush)?.Visual is Visual content) + if ((rectangle.Fill as VisualBrush)?.Visual is { } content) { var flowDirection = content.VisualParent?.FlowDirection ?? FlowDirection.LeftToRight; rectangle.FlowDirection = flowDirection;