Skip to content

Commit

Permalink
ComboBox Empty Selection should not Generate a TextBlock as Selection…
Browse files Browse the repository at this point in the history
…BoxItem (#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]>
  • Loading branch information
gehongyan and MrJul authored Oct 7, 2024
1 parent b11574a commit 770d31f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private void UpdateSelectionBoxItem(object? item)
}
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
Expand Down
19 changes: 19 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,24 @@ public void SelectionBoxItemTemplate_Inherits_From_ItemTemplate_When_ItemTemplat

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

0 comments on commit 770d31f

Please sign in to comment.