本文整理汇总了C#中ObservableList.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableList.Clear方法的具体用法?C# ObservableList.Clear怎么用?C# ObservableList.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableList
的用法示例。
在下文中一共展示了ObservableList.Clear方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LocationsTreeViewModel
public LocationsTreeViewModel(ObservableCollection<Location> locations)
{
Locations = new ObservableList<LocationNode>();
foreach (var location in locations) Locations.Add(new LocationNode(location));
locations.CollectionChanged += (s, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (Location location in e.NewItems) Locations.Add(new LocationNode(location));
break;
case NotifyCollectionChangedAction.Remove:
foreach (Location location in e.OldItems)
{
var item = Locations.FirstOrDefault(l => l.Location.Guid == location.Guid);
if (item != null) Locations.Remove(item);
}
break;
case NotifyCollectionChangedAction.Replace:
foreach (Location location in e.OldItems)
{
var item = Locations.FirstOrDefault(l => l.Location.Guid == location.Guid);
if (item != null) Locations.Remove(item);
}
foreach (Location location in e.NewItems) Locations.Add(new LocationNode(location));
break;
case NotifyCollectionChangedAction.Reset:
Locations.Clear();
foreach (Location location in e.NewItems) Locations.Add(new LocationNode(location));
break;
}
};
LocationsTreeViewSource = new CollectionViewSource { Source = Locations };
LocationsTreeViewSource.SortDescriptions.Add(new SortDescription("Location.Name", ListSortDirection.Ascending));
}
示例2: CanAccumulate
public void CanAccumulate()
{
var items = new ObservableList<Person>();
var subscription = items.Changes
.Subscribe(changes =>
{
Console.WriteLine(changes);
});
using (var x = items.SuspendNotifications())
{
foreach (var person in _random.Take(20000))
{
items.Add(person);
}
items.Clear();
var result = items.GetChanges();
items[10] = new Person("Roland", 1);
}
}
示例3: ClearFiresListClearedEvent
public void ClearFiresListClearedEvent()
{
var didFire = false;
var observableList = new ObservableList<string> { "it doesn't matter" };
observableList.ListCleared += (s, e) => { didFire = true; };
observableList.Clear();
Assert.IsTrue(didFire);
}
示例4: ObservableList_RaisesCollectionResetOnClear
public void ObservableList_RaisesCollectionResetOnClear()
{
var list = new ObservableList<Int32>();
var reset = false;
list.CollectionReset += (source) =>
{
reset = true;
};
list.Add(1234);
list.Clear();
TheResultingValue(reset).ShouldBe(true);
}
示例5: Clearing_the_list_raises_property_changed
public void Clearing_the_list_raises_property_changed()
{
var observable = new ObservableList<string>();
var propertyChangedRaised = false;
observable.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "Clear")
propertyChangedRaised = true;
};
observable.Clear();
Assert.IsTrue(propertyChangedRaised);
}
示例6: When_Clear_Then_Action_Reset_Raised
public void When_Clear_Then_Action_Reset_Raised()
{
// Arrange
var sut = new ObservableList<int>(new[] {10, 12, 18, 100});
var results = new List<NotifyCollectionChangedEventArgs>();
var expected = new[]
{
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset),
};
sut.CollectionChanged += (o, e) => results.Add(e);
// Act
sut.Clear();
// Assert
KKAssert.AreEqualByValue(expected, results);
}
示例7: event_raised_when_list_is_cleared
public void event_raised_when_list_is_cleared()
{
var testObject = new ObservableList<int>();
var events = new List<NotifyCollectionChangedEventArgs>();
testObject.Add(1);
testObject.Add(2);
testObject.CollectionChanged += (source, args) => events.Add(args);
testObject.Clear();
Assert.Equal(1, events.Count);
var ev = events[0];
Assert.Equal(NotifyCollectionChangedAction.Reset, ev.Action);
Assert.Equal(2, ev.OldItems.Count);
Assert.Equal(1, ev.OldItems[0]);
Assert.Equal(2, ev.OldItems[1]);
Assert.Equal(0, ev.OldStartingIndex);
}
示例8: ClearWithNoEventListenerClearsList
public void ClearWithNoEventListenerClearsList()
{
var observableList = new ObservableList<DateTime> { DateTime.Now, new DateTime(2001, 5, 4) };
observableList.Clear();
CollectionAssert.IsEmpty(observableList.ToArray());
}
示例9: TestClear
public void TestClear()
{
var list = new List<string> { "aaa", "bbb", "ccc" };
var set = new ObservableList<string>(list);
Assert.AreEqual(set.Count, list.Count);
bool propertyChangedInvoked = false;
bool collectionChangedInvoked = false;
set.PropertyChanged += (sender, e) =>
{
Assert.AreEqual(e.PropertyName, nameof(ObservableList<string>.Count));
propertyChangedInvoked = true;
};
set.CollectionChanged += (sender, e) =>
{
Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Reset);
collectionChangedInvoked = true;
};
set.Clear();
Assert.AreEqual(set.Count, 0);
Assert.True(propertyChangedInvoked);
Assert.True(collectionChangedInvoked);
}