protected virtual void OnGeneratorItemsChanged(object sender, ItemsChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { for (int i = 0; i < e.ItemsCount; i++) { Children.Insert(i + e.NewStartingIndex, ItemContainerGenerator.Generate(i + e.NewStartingIndex)); } } if (e.Action == NotifyCollectionChangedAction.Remove) { Children.RemoveRange(e.OldStartingIndex, e.ContainersCount); } if (e.Action == NotifyCollectionChangedAction.Move) { IEnumerable <UIElement> movedChildren = Children.Skip(e.OldStartingIndex).Take(e.ContainersCount); Children.RemoveRange(e.OldStartingIndex, e.ContainersCount); Children.InsertRange(e.NewStartingIndex, movedChildren); } if (e.Action == NotifyCollectionChangedAction.Replace) { Children[e.NewStartingIndex] = ItemContainerGenerator.Generate(e.NewStartingIndex); } if (e.Action == NotifyCollectionChangedAction.Reset) { Children.Clear(); AddChildren(); } }
private void AddChildren() { for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++) { Children.Add(ItemContainerGenerator.Generate(i)); } }
public void ItemContainerGeneratorBasicTest() { TestGeneratorHost host = new TestGeneratorHost(); ItemContainerGenerator generator = new ItemContainerGenerator(host); ItemsChangedEventArgs lastChangedArg = null; generator.ItemsChanged += (sender, e) => lastChangedArg = e; host.View.Add("item1"); Assert.AreEqual(NotifyCollectionChangedAction.Add, lastChangedArg.Action); Assert.AreEqual(0, lastChangedArg.NewStartingIndex); Assert.AreEqual(1, lastChangedArg.ItemsCount); Assert.AreEqual(0, lastChangedArg.ContainersCount); FrameworkElement container1 = generator.Generate(0); Assert.IsTrue(host.Containers.ContainsKey("item1")); Assert.AreEqual("item1", container1.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); host.View[0] = "item2"; Assert.IsFalse(host.Containers.ContainsKey("item1")); Assert.IsTrue(host.Containers.ContainsKey("item2")); Assert.IsNull(container1.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); FrameworkElement container2 = generator.Generate(0); Assert.IsTrue(host.Containers.ContainsKey("item2")); Assert.AreEqual("item2", container2.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); host.View.Remove("item2"); Assert.IsFalse(host.Containers.ContainsKey("item2")); Assert.IsNull(container2.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); host.View.Add("item3"); Assert.IsFalse(host.Containers.ContainsKey("item3")); FrameworkElement container3 = generator.Generate(0); Assert.IsTrue(host.Containers.ContainsKey("item3")); Assert.AreEqual("item3", container3.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); generator.Dispose(); Assert.IsFalse(host.Containers.ContainsKey("item3")); Assert.IsNull(container3.GetValue(TestGeneratorHost.ItemForItemContainerProperty)); }
private void SetSingleSelection(ListBoxItem item) { SelectedItem = ItemContainerGenerator.ItemFromContainer(item); for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++) { DependencyObject itemContainer = ItemContainerGenerator.Generate(i); itemContainer.SetCurrentValue(Selector.IsSelectedProperty, itemContainer == item); } }
private void ToggleSingleSelection(ListBoxItem item) { bool isSelected = !(bool)item.GetValue(Selector.IsSelectedProperty); SelectedItem = isSelected ? ItemContainerGenerator.ItemFromContainer(item) : null; for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++) { DependencyObject itemContainer = ItemContainerGenerator.Generate(i); itemContainer.SetCurrentValue(Selector.IsSelectedProperty, itemContainer == item && isSelected); } }
private void SetRangeSelection(ListBoxItem item) { int itemIndex = ItemContainerGenerator.IndexFromContainer(item); int selectionAnchorIndex = ItemContainerGenerator.IndexFromContainer(selectionAnchor); int rangeStartIndex = itemIndex.Min(selectionAnchorIndex); int rangeEndIndex = itemIndex.Max(selectionAnchorIndex); for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++) { DependencyObject itemContainer = ItemContainerGenerator.Generate(i); itemContainer.SetCurrentValue(Selector.IsSelectedProperty, rangeStartIndex <= i && i <= rangeEndIndex); } }