public void Removing_Selected_Item_Should_Clear_Selection() { var items = new AvaloniaList <Item> { new Item(), new Item(), }; var target = new SelectingItemsControl { Items = items, Template = Template(), }; Prepare(target); target.SelectedIndex = 1; Assert.Equal(items[1], target.SelectedItem); Assert.Equal(1, target.SelectedIndex); SelectionChangedEventArgs receivedArgs = null; target.SelectionChanged += (_, args) => receivedArgs = args; var removed = items[1]; items.RemoveAt(1); Assert.Null(target.SelectedItem); Assert.Equal(-1, target.SelectedIndex); Assert.NotNull(receivedArgs); Assert.Empty(receivedArgs.AddedItems); Assert.Equal(new[] { removed }, receivedArgs.RemovedItems); Assert.False(items.Single().IsSelected); }
public void Removing_Selected_Item_Should_Clear_Selection_With_BeginInit() { var items = new AvaloniaList <Item> { new Item(), new Item(), }; var target = new SelectingItemsControl(); target.BeginInit(); target.Items = items; target.Template = Template(); target.EndInit(); target.ApplyTemplate(); target.Presenter.ApplyTemplate(); target.SelectedIndex = 0; Assert.Equal(items[0], target.SelectedItem); Assert.Equal(0, target.SelectedIndex); SelectionChangedEventArgs receivedArgs = null; target.SelectionChanged += (_, args) => receivedArgs = args; var removed = items[0]; items.RemoveAt(0); Assert.Null(target.SelectedItem); Assert.Equal(-1, target.SelectedIndex); Assert.NotNull(receivedArgs); Assert.Empty(receivedArgs.AddedItems); Assert.Equal(new[] { removed }, receivedArgs.RemovedItems); Assert.False(items.Single().IsSelected); }