protected override void Initialize(AvaloniaList <TreeNode> nodes) { _subscription = _control.VisualChildren.ForEachItem( (i, item) => nodes.Insert(i, new VisualTreeNode(item, Owner)), (i, item) => nodes.RemoveAt(i), () => nodes.Clear()); }
public void Inserting_Then_Removing_Should_Add_Remove_Containers() { var items = new AvaloniaList <string>(Enumerable.Range(0, 5).Select(x => $"Item {x}")); var toAdd = Enumerable.Range(0, 3).Select(x => $"Added Item {x}").ToArray(); var target = new ItemsPresenter { VirtualizationMode = ItemVirtualizationMode.None, Items = items, ItemTemplate = new FuncDataTemplate <string>(x => new TextBlock { Height = 10 }), }; target.ApplyTemplate(); Assert.Equal(items.Count, target.Panel.Children.Count); int addIndex = 1; foreach (var item in toAdd) { items.Insert(addIndex++, item); } Assert.Equal(items.Count, target.Panel.Children.Count); foreach (var item in toAdd) { items.Remove(item); } Assert.Equal(items.Count, target.Panel.Children.Count); }
public void CreateDerivedList_Handles_Insert() { var source = new AvaloniaList <int>(new[] { 0, 1, 2, 3 }); var target = source.CreateDerivedList(x => new Wrapper(x)); source.Insert(1, 4); var result = target.Select(x => x.Value).ToList(); Assert.Equal(source, result); }
public void Inserting_Item_Should_Raise_CollectionChanged() { var target = new AvaloniaList <int>(new[] { 1, 2 }); var raised = false; target.CollectionChanged += (s, e) => { Assert.Equal(target, s); Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); Assert.Equal(new[] { 3 }, e.NewItems.Cast <int>()); Assert.Equal(1, e.NewStartingIndex); raised = true; }; target.Insert(1, 3); Assert.True(raised); }
public void Inserting_Then_Removing_Should_Add_Remove_Containers() { var items = new AvaloniaList<string>(Enumerable.Range(0, 5).Select(x => $"Item {x}")); var toAdd = Enumerable.Range(0, 3).Select(x => $"Added Item {x}").ToArray(); var target = new ItemsPresenter { VirtualizationMode = ItemVirtualizationMode.None, Items = items, ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }), }; target.ApplyTemplate(); Assert.Equal(items.Count, target.Panel.Children.Count); foreach (var item in toAdd) { items.Insert(1, item); } Assert.Equal(items.Count, target.Panel.Children.Count); foreach (var item in toAdd) { items.Remove(item); } Assert.Equal(items.Count, target.Panel.Children.Count); }
public void LayoutManager_Should_Measure_Arrange_All() { var virtualizationMode = ItemVirtualizationMode.Simple; using (UnitTestApplication.Start(TestServices.StyledWindow)) { var items = new AvaloniaList <string>(Enumerable.Range(1, 7).Select(v => v.ToString())); var wnd = new Window() { SizeToContent = SizeToContent.WidthAndHeight }; wnd.IsVisible = true; var target = new ListBox(); wnd.Content = target; var lm = wnd.LayoutManager; target.Height = 110; target.Width = 50; target.DataContext = items; target.VirtualizationMode = virtualizationMode; target.ItemTemplate = new FuncDataTemplate <object>((c, _) => { var tb = new TextBlock() { Height = 10, Width = 30 }; tb.Bind(TextBlock.TextProperty, new Data.Binding()); return(tb); }, true); lm.ExecuteInitialLayoutPass(); target.Items = items; lm.ExecuteLayoutPass(); items.Insert(3, "3+"); lm.ExecuteLayoutPass(); items.Insert(4, "4+"); lm.ExecuteLayoutPass(); //RESET items.Clear(); foreach (var i in Enumerable.Range(1, 7)) { items.Add(i.ToString()); } //working bit better with this line no outof memory or remaining to arrange/measure ??? //lm.ExecuteLayoutPass(); items.Insert(2, "2+"); lm.ExecuteLayoutPass(); //after few more layout cycles layoutmanager shouldn't hold any more visual for measure/arrange lm.ExecuteLayoutPass(); lm.ExecuteLayoutPass(); var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic; var toMeasure = lm.GetType().GetField("_toMeasure", flags).GetValue(lm) as System.Collections.Generic.IEnumerable <Layout.ILayoutable>; var toArrange = lm.GetType().GetField("_toArrange", flags).GetValue(lm) as System.Collections.Generic.IEnumerable <Layout.ILayoutable>; Assert.Equal(0, toMeasure.Count()); Assert.Equal(0, toArrange.Count()); } }
public void Inserting_Item_Should_Raise_CollectionChanged() { var target = new AvaloniaList<int>(new[] { 1, 2 }); var raised = false; target.CollectionChanged += (s, e) => { Assert.Equal(target, s); Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>()); Assert.Equal(1, e.NewStartingIndex); raised = true; }; target.Insert(1, 3); Assert.True(raised); }