本文整理汇总了C#中System.Windows.Controls.ItemCollection.SetItemsSource方法的典型用法代码示例。如果您正苦于以下问题:C# ItemCollection.SetItemsSource方法的具体用法?C# ItemCollection.SetItemsSource怎么用?C# ItemCollection.SetItemsSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.ItemCollection
的用法示例。
在下文中一共展示了ItemCollection.SetItemsSource方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ItemCollectionItemsSourceTest
public void ItemCollectionItemsSourceTest()
{
ObservableCollection<string> collection = new ObservableCollection<string>(new[] { "collection1", "collection2", "collection3" });
InnerCollectionView view = new InnerCollectionView();
view.Add("view1");
view.Add("view2");
view.Add("view3");
view.CurrentItemIndex = 1;
ItemCollection itemCollection = new ItemCollection();
NotifyCollectionChangedEventArgs lastChangeArgs = null;
int currentChangedCount = 0;
itemCollection.CollectionChanged += (sender, e) => lastChangeArgs = e;
itemCollection.CurrentChanged += (sender, e) => currentChangedCount++;
itemCollection.SetItemsSource(collection);
Assert.AreEqual(-1, itemCollection.CurrentItemIndex);
Assert.AreEqual(1, currentChangedCount);
collection.Add("collection4");
Assert.AreEqual(NotifyCollectionChangedAction.Add, lastChangeArgs.Action);
CollectionAssert.AreEqual(new[] { "collection4" }, lastChangeArgs.NewItems.Cast<string>().ToArray());
Assert.AreEqual(-1, itemCollection.CurrentItemIndex);
Assert.AreEqual(1, currentChangedCount);
itemCollection.CurrentItemIndex = 0;
Assert.AreEqual(0, itemCollection.CurrentItemIndex);
Assert.AreEqual(2, currentChangedCount);
itemCollection.SetItemsSource(view);
Assert.AreEqual(NotifyCollectionChangedAction.Reset, lastChangeArgs.Action);
CollectionAssert.AreEqual(new[] { "collection1", "collection2", "collection3", "collection4" }, lastChangeArgs.OldItems.Cast<string>().ToArray());
CollectionAssert.AreEqual(new[] { "view1", "view2", "view3" }, lastChangeArgs.NewItems.Cast<string>().ToArray());
Assert.AreEqual(1, itemCollection.CurrentItemIndex);
Assert.AreEqual(3, currentChangedCount);
itemCollection.SetItemsSource(collection);
lastChangeArgs = null;
view.Add("view4");
Assert.IsNull(lastChangeArgs);
}