Skip to content

Commit

Permalink
Merge pull request #4304 from AvaloniaUI/revert-4294-fixes/4293-listb…
Browse files Browse the repository at this point in the history
…ox-remove-item-selection

Revert "Fix selection after deleting an item."
  • Loading branch information
grokys authored Jul 16, 2020
2 parents 06d112e + 4183106 commit 5004606
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 71 deletions.
8 changes: 1 addition & 7 deletions samples/ControlCatalog/Pages/ListBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
HorizontalAlignment="Center"
Spacing="16">
<StackPanel Orientation="Vertical" Spacing="8">
<ListBox Items="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
SelectedItems="{Binding SelectedItems}"
AutoScrollToSelectedItem="True"
SelectionMode="{Binding SelectionMode}"
Width="250"
Height="350"/>
<ListBox Items="{Binding Items}" SelectedItem="{Binding SelectedItem}" AutoScrollToSelectedItem="True" SelectionMode="{Binding SelectionMode}" Width="250" Height="350"></ListBox>

<Button Command="{Binding AddItemCommand}">Add</Button>

Expand Down
11 changes: 2 additions & 9 deletions src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static ItemsControl()
public ItemsControl()
{
PseudoClasses.Add(":empty");
SubscribeToItems(_items);
}

/// <summary>
Expand Down Expand Up @@ -264,11 +265,6 @@ protected virtual void OnContainersRecycled(ItemContainerEventArgs e)
{
}

protected override void OnInitialized()
{
SubscribeToItems(_items);
}

/// <summary>
/// Handles directional navigation within the <see cref="ItemsControl"/>.
/// </summary>
Expand Down Expand Up @@ -334,10 +330,7 @@ protected virtual void ItemsChanged(AvaloniaPropertyChangedEventArgs e)
Presenter.Items = newValue;
}

if (IsInitialized)
{
SubscribeToItems(newValue);
}
SubscribeToItems(newValue);
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions tests/Avalonia.Controls.UnitTests/CarouselTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit;

Expand Down Expand Up @@ -156,7 +155,6 @@ public void Selected_Item_Changes_To_First_Item_When_Item_Added()
IsVirtualized = false
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down Expand Up @@ -249,7 +247,6 @@ public void Selected_Item_Changes_To_Next_First_Item_When_Item_Removed_From_Begg
IsVirtualized = false
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down
4 changes: 0 additions & 4 deletions tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public void Control_Item_Should_Be_Removed_From_Logical_Children_Before_ApplyTem
var child = new Control();
var items = new AvaloniaList<Control>(child);

var root = new TestRoot(target);
target.Template = GetTemplate();
target.Items = items;
items.RemoveAt(0);
Expand Down Expand Up @@ -284,7 +283,6 @@ public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
var items = new AvaloniaList<string> { "Foo" };
var called = false;

var root = new TestRoot(target);
target.Template = GetTemplate();
target.Items = items;
target.ApplyTemplate();
Expand All @@ -305,7 +303,6 @@ public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged()
var items = new AvaloniaList<string> { "Foo", "Bar" };
var called = false;

var root = new TestRoot(target);
target.Template = GetTemplate();
target.Items = items;
target.ApplyTemplate();
Expand Down Expand Up @@ -379,7 +376,6 @@ public void Empty_Class_Should_Be_Cleared_When_Items_Added()
Items = new[] { 1, 2, 3 },
};

var root = new TestRoot(target);
Assert.DoesNotContain(":empty", target.Classes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ public void Setting_SelectedIndex_During_Initialize_Should_Select_Item_When_Alwa
SelectionMode = SelectionMode.Single | SelectionMode.AlwaysSelected
};

var root = new TestRoot(listBox);

listBox.BeginInit();

listBox.SelectedIndex = 1;
Expand Down Expand Up @@ -482,7 +480,6 @@ public void Adding_Selected_Item_Should_Update_Selection()
Template = Template(),
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
items.Add(new Item { IsSelected = true });
Expand Down Expand Up @@ -534,7 +531,6 @@ public void Removing_Selected_Item_Should_Clear_Selection()
};

target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.SelectedIndex = 1;

Assert.Equal(items[1], target.SelectedItem);
Expand All @@ -553,45 +549,6 @@ public void Removing_Selected_Item_Should_Clear_Selection()
Assert.NotNull(receivedArgs);
Assert.Empty(receivedArgs.AddedItems);
Assert.Equal(new[] { removed }, receivedArgs.RemovedItems);
Assert.False(items.Single().IsSelected);
}

[Fact]
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);
}

[Fact]
Expand Down Expand Up @@ -922,7 +879,6 @@ public void Removing_SelectedItem_Should_Clear_TabOnceActiveElement()
Items = items,
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,6 @@ public void Adding_Item_Before_SelectedItems_Should_Update_Selection()
SelectionMode = SelectionMode.Multiple,
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down Expand Up @@ -1044,7 +1043,6 @@ public void Removing_Item_Before_SelectedItem_Should_Update_Selection()
SelectionMode = SelectionMode.Multiple,
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down Expand Up @@ -1078,7 +1076,6 @@ public void Removing_SelectedItem_With_Multiple_Selection_Active_Should_Update_S
SelectionMode = SelectionMode.Multiple,
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();

Expand Down Expand Up @@ -1202,7 +1199,6 @@ public void Adding_Selected_ItemContainers_Should_Update_Selection()
Template = Template(),
};

var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
items.Add(new ItemContainer { IsSelected = true });
Expand Down

0 comments on commit 5004606

Please sign in to comment.