Browse Source

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 <julien@lebosquain.net>
pull/17211/head
Ge 1 year ago
committed by GitHub
parent
commit
770d31f4e6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/Avalonia.Controls/ComboBox.cs
  2. 19
      tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

2
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
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);
}
}
}

Loading…
Cancel
Save