public void When_TemplateRootIsOwnContainer() { var count = 0; var panel = new StackPanel(); var SUT = new MyItemsControl() { ItemsPanelRoot = panel, ItemTemplate = new DataTemplate(() => { count++; return(new MyCustomItemContainer() { MyValue = 42 }); }), Template = new ControlTemplate(() => new ItemsPresenter()), }; SUT.ApplyTemplate(); SUT.ItemsSource = new object[] { 42 }; var container = SUT.ContainerFromIndex(0) as MyCustomItemContainer; Assert.IsNotNull(container); Assert.IsTrue(container.IsGeneratedContainer); Assert.IsFalse(container.ContentTemplateRoot is MyCustomItemContainer); Assert.AreEqual(42, container.DataContext); }
public void When_ObservableCollection() { var count = 0; var panel = new StackPanel(); var itemsPresenter = new MyItemsControl(); var itemTemplate = new DataTemplate(() => { count++; return(new Border()); }); var SUT = new MyItemsControl() { ItemsPanelRoot = panel, ItemTemplate = itemTemplate, Template = new ControlTemplate(() => new ItemsPresenter()), }; SUT.ApplyTemplate(); Assert.AreEqual(0, count); var collection = new ObservableCollection <int>(); SUT.ItemsSource = collection; Assert.AreEqual(0, count); collection.Add(1); Assert.AreEqual(1, count); collection.Add(42); Assert.AreEqual(2, count); collection.Add(43); Assert.AreEqual(3, count); collection.RemoveAt(0); Assert.AreEqual(3, count); collection[0] = 44; Assert.AreEqual(FeatureConfiguration.FrameworkTemplate.IsPoolingEnabled ? 3 : 4, count); }
private static void OnItemsSourcePropertyChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e) { MyItemsControl ic = obj as MyItemsControl; if (ic != null) { var oldCollectionChanged = e.OldValue as INotifyCollectionChanged; var newCollectionChanged = e.NewValue as INotifyCollectionChanged; if (oldCollectionChanged != null) { oldCollectionChanged.CollectionChanged -= ic.OnItemsCollectionChanged; } if (newCollectionChanged != null) { newCollectionChanged.CollectionChanged += ic.OnItemsCollectionChanged; // in addition to adding a CollectionChanged handler // any already existing collection elements should be processed here } } }