|
|
@@ -454,6 +454,118 @@ namespace Avalonia.Controls.UnitTests
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void ListBox_Presenter_Items_Should_Be_In_Sync_When_Replacing_Items()
|
|
|
+ {
|
|
|
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
|
+ {
|
|
|
+ var wnd = new Window() { Width = 100, Height = 100, IsVisible = true };
|
|
|
+
|
|
|
+ var target = new ListBox()
|
|
|
+ {
|
|
|
+ VerticalAlignment = Layout.VerticalAlignment.Top,
|
|
|
+ AutoScrollToSelectedItem = true,
|
|
|
+ Width = 50,
|
|
|
+ VirtualizationMode = ItemVirtualizationMode.Simple,
|
|
|
+ Items = new[]
|
|
|
+ {
|
|
|
+ new Item("Item1")
|
|
|
+ },
|
|
|
+ };
|
|
|
+ wnd.Content = target;
|
|
|
+
|
|
|
+ var lm = wnd.LayoutManager;
|
|
|
+
|
|
|
+ lm.ExecuteInitialLayoutPass();
|
|
|
+
|
|
|
+ int dematerializedEventCallCount = 0;
|
|
|
+ target.ItemContainerGenerator.Dematerialized += (s, e) =>
|
|
|
+ {
|
|
|
+ Assert.IsType<Item>(e.Containers[0].Item);
|
|
|
+ Assert.Equal("Item1", ((Item)e.Containers[0].Item).Value);
|
|
|
+ dematerializedEventCallCount++;
|
|
|
+ };
|
|
|
+
|
|
|
+ int materializedEventCallCount = 0;
|
|
|
+ target.ItemContainerGenerator.Materialized += (s, e) =>
|
|
|
+ {
|
|
|
+ Assert.IsType<Item>(e.Containers[0].Item);
|
|
|
+ Assert.Equal("Item2", ((Item)e.Containers[0].Item).Value);
|
|
|
+ materializedEventCallCount++;
|
|
|
+ };
|
|
|
+
|
|
|
+ target.Items = new[]
|
|
|
+ {
|
|
|
+ new Item("Item2")
|
|
|
+ };
|
|
|
+
|
|
|
+ //assert that materialize/dematerialize events are called exactly one time
|
|
|
+ Assert.Equal(1, dematerializedEventCallCount);
|
|
|
+ Assert.Equal(1, materializedEventCallCount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void ListBox_Items_Should_Be_In_Sync_When_Replacing_ItemTemplate()
|
|
|
+ {
|
|
|
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
|
+ {
|
|
|
+ var wnd = new Window() { Width = 100, Height = 100, IsVisible = true };
|
|
|
+
|
|
|
+ var target = new ListBox()
|
|
|
+ {
|
|
|
+ VerticalAlignment = Layout.VerticalAlignment.Top,
|
|
|
+ AutoScrollToSelectedItem = true,
|
|
|
+ Width = 50,
|
|
|
+ VirtualizationMode = ItemVirtualizationMode.Simple,
|
|
|
+ Items = new[]
|
|
|
+ {
|
|
|
+ new Item("Item1")
|
|
|
+ },
|
|
|
+ ItemTemplate =
|
|
|
+ new FuncDataTemplate<Item>((x, ns) => new Canvas())
|
|
|
+ };
|
|
|
+
|
|
|
+ wnd.Content = target;
|
|
|
+
|
|
|
+ var lm = wnd.LayoutManager;
|
|
|
+
|
|
|
+ lm.ExecuteInitialLayoutPass();
|
|
|
+
|
|
|
+ int dematerializedEventCallCount = 0;
|
|
|
+ target.ItemContainerGenerator.Dematerialized += (s, e) =>
|
|
|
+ {
|
|
|
+ Assert.IsType<Item>(e.Containers[0].Item);
|
|
|
+ Assert.Equal("Item1", ((Item)e.Containers[0].Item).Value);
|
|
|
+ Assert.IsType<Canvas>(((ListBoxItem)e.Containers[0].ContainerControl).Presenter.Child);
|
|
|
+ dematerializedEventCallCount++;
|
|
|
+ };
|
|
|
+
|
|
|
+ int materializedEventCallCount = 0;
|
|
|
+ ListBoxItem materializedListBoxItem = null;
|
|
|
+ target.ItemContainerGenerator.Materialized += (s, e) =>
|
|
|
+ {
|
|
|
+ Assert.IsType<Item>(e.Containers[0].Item);
|
|
|
+ Assert.Equal("Item1", ((Item)e.Containers[0].Item).Value);
|
|
|
+ materializedListBoxItem = ((ListBoxItem)e.Containers[0].ContainerControl);
|
|
|
+ materializedEventCallCount++;
|
|
|
+ };
|
|
|
+
|
|
|
+ target.ItemTemplate =
|
|
|
+ new FuncDataTemplate<Item>((x, ns) => new TextBlock());
|
|
|
+
|
|
|
+ //ensure events are called only one time
|
|
|
+ Assert.Equal(1, dematerializedEventCallCount);
|
|
|
+ Assert.Equal(1, materializedEventCallCount);
|
|
|
+
|
|
|
+ wnd.LayoutManager.ExecuteLayoutPass();
|
|
|
+
|
|
|
+ //ensure that new template has been applied
|
|
|
+ Assert.IsType<TextBlock>(materializedListBoxItem.Presenter.Child);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private FuncControlTemplate ListBoxTemplate()
|
|
|
{
|
|
|
return new FuncControlTemplate<ListBox>((parent, scope) =>
|