فهرست منبع

ComboBox Empty Selection should not Generate a TextBlock as SelectionBoxItem (#16748)

* ComboBox empty selection should not generate a TextBlock as SelectionBoxItem

Fixes: #16747

* Add test for ComboBox DisplayMemberBinding behavior without selection.

---------

Co-authored-by: Julien Lebosquain <[email protected]>
Ge 1 سال پیش
والد
کامیت
770d31f4e6
2فایلهای تغییر یافته به همراه20 افزوده شده و 1 حذف شده
  1. 1 1
      src/Avalonia.Controls/ComboBox.cs
  2. 19 0
      tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

+ 1 - 1
src/Avalonia.Controls/ComboBox.cs

@@ -471,7 +471,7 @@ namespace Avalonia.Controls
             }
             else
             {
-                if(ItemTemplate is null && SelectionBoxItemTemplate is null && DisplayMemberBinding is { } binding)
+                if (item is not null && ItemTemplate is null && SelectionBoxItemTemplate is null && DisplayMemberBinding is { } binding)
                 {
                     var template = new FuncDataTemplate<object?>((_, _) =>
                     new TextBlock

+ 19 - 0
tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

@@ -517,5 +517,24 @@ namespace Avalonia.Controls.UnitTests
 
             Assert.Equal(itemTemplate2, target.SelectionBoxItemTemplate);
         }
+
+        [Fact]
+        public void DisplayMemberBinding_Is_Not_Applied_To_SelectionBoxItem_Without_Selection()
+        {
+            var target = new ComboBox
+            {
+                DisplayMemberBinding = new Binding(),
+                ItemsSource = new[] { "foo", "bar" }
+            };
+
+            target.SelectedItem = null;
+            Assert.Null(target.SelectionBoxItem);
+
+            target.SelectedItem = "foo";
+            Assert.NotNull(target.SelectionBoxItem);
+
+            target.SelectedItem = null;
+            Assert.Null(target.SelectionBoxItem);
+        }
     }
 }