本文整理汇总了C#中ObservableCollection.ObservableSelectMany方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableCollection.ObservableSelectMany方法的具体用法?C# ObservableCollection.ObservableSelectMany怎么用?C# ObservableCollection.ObservableSelectMany使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableCollection
的用法示例。
在下文中一共展示了ObservableCollection.ObservableSelectMany方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ObservableSelectMany_ChangeItemValuesTest
public void ObservableSelectMany_ChangeItemValuesTest()
{
var collection = new ObservableCollection<TestObject>();
var values = collection.ObservableSelectMany(item => item.Inner);
var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);
var expectedInitialValues = new[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
var expectedValues = new[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Replace, 9) };
var testObject1 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
var testObject2 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
var testObject3 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
{
receivedEvents[e.Action] += 1;
};
collection.AddRange(testObject1, testObject2, testObject3);
Assert.IsTrue(values.SequenceEqual(expectedInitialValues));
values.CollectionChanged += collectionChangedEventHandler;
var innerValue = 1;
foreach (var item in collection)
{
var subItems = item.Inner;
for (var i = 0; i < subItems.Count; i++)
{
subItems[i] = ++innerValue;
}
}
Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
Assert.IsTrue(values.SequenceEqual(expectedValues));
}
示例2: ObservableSelectMany_RemoveValuesTest
public void ObservableSelectMany_RemoveValuesTest()
{
var collection = new ObservableCollection<TestObject>();
var values = collection.ObservableSelectMany(item => item.Inner);
var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);
var expectedInitialValues = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var expectedValues = new[] { 1, 3, 4, 6, 7, 9 };
var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Remove, 3) };
var testObject1 = new TestObject { Inner = new ObservableCollection<int>(new[] { 1, 2, 3 }) };
var testObject2 = new TestObject { Inner = new ObservableCollection<int>(new[] { 4, 5, 6 }) };
var testObject3 = new TestObject { Inner = new ObservableCollection<int>(new[] { 7, 8, 9 }) };
NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
{
receivedEvents[e.Action] += 1;
};
collection.AddRange(testObject1, testObject2, testObject3);
Assert.IsTrue(values.SequenceEqual(expectedInitialValues));
values.CollectionChanged += collectionChangedEventHandler;
foreach (var item in collection)
{
item.Inner.RemoveAt(1);
}
Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
Assert.IsTrue(values.SequenceEqual(expectedValues));
}
示例3: ObservableSelectMany_AddItemsTest
public void ObservableSelectMany_AddItemsTest()
{
var collection = new ObservableCollection<TestObject>();
var values = collection.ObservableSelectMany(item => item.Inner);
var receivedEvents = new ObservableIndexer<NotifyCollectionChangedAction, int>(_ => 0);
var expectedValues = new[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
var expectedEvents = new[] { KeyValuePair(NotifyCollectionChangedAction.Add, 9) };
var testObject1 = new TestObject { Inner = new ObservableCollection<int>() };
var testObject2 = new TestObject { Inner = new ObservableCollection<int>() };
var testObject3 = new TestObject { Inner = new ObservableCollection<int>() };
NotifyCollectionChangedEventHandler collectionChangedEventHandler = (sender, e) =>
{
receivedEvents[e.Action] += 1;
};
values.CollectionChanged += collectionChangedEventHandler;
Assert.AreEqual(0, values.Count);
collection.AddRange(testObject1, testObject2, testObject3);
foreach (var item in collection)
{
item.Inner.AddRange(1, 2, 3);
}
Assert.IsTrue(receivedEvents.SequenceEqual(expectedEvents));
Assert.IsTrue(values.SequenceEqual(expectedValues));
}