本文整理汇总了C#中ObservableList.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableList.Remove方法的具体用法?C# ObservableList.Remove怎么用?C# ObservableList.Remove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableList
的用法示例。
在下文中一共展示了ObservableList.Remove方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: Click
void Click()
{
if (click==0)
{
items = listView.DataSource;
items.Add("Added from script 0");
items.Add("Added from script 1");
items.Add("Added from script 2");
items.Remove("Caster");
click += 1;
return ;
}
if (click==1)
{
items.Clear();
click += 1;
return ;
}
if (click==2)
{
items.Add("test");
click += 1;
return ;
}
}
示例3: RemoveItem
public void RemoveItem()
{
_handle = new EventWaitHandle(false, EventResetMode.ManualReset);
var test = new ObservableList<string>();
test.Add("myValue");
test.ItemRemoved += List_ItemEvent;
test.Remove("myValue");
Assert.IsTrue(_handle.WaitOne(10));
}
示例4: ObservableList_RaisesCollectionItemRemoved
public void ObservableList_RaisesCollectionItemRemoved()
{
var list = new ObservableList<Int32>();
var removed = false;
list.CollectionItemRemoved += (source, index, value) =>
{
removed = (value == 1234);
};
list.Add(1234);
list.Remove(1234);
TheResultingValue(removed).ShouldBe(true);
}
示例5: Removing_an_item_from_the_list_raises_property_changed
public void Removing_an_item_from_the_list_raises_property_changed()
{
const string item = "test";
var observable = new ObservableList<string> { item };
var propertyChangedRaised = false;
observable.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "Remove")
propertyChangedRaised = true;
};
observable.Remove(item);
Assert.IsTrue(propertyChangedRaised);
}
示例6: TestAddRemoveDispose
public void TestAddRemoveDispose()
{
var sourceList = new ObservableList<string>();
var targetList = new Collection<string>();
var syncer1 = new CollectionSyncer<string, string>(sourceList, targetList, (x) => x.ToUpper(), (x) => x.ToUpper(), false);
var syncer2 = new CollectionSyncer<string, string>(sourceList, targetList, (x) => x.ToLower(), (x) => x.ToLower(), false);
sourceList.Add("Test1");
Assert.Equal(2, targetList.Count);
Assert.True(targetList.Contains("test1"));
Assert.True(targetList.Contains("TEST1"));
sourceList.Remove("Test1");
Assert.Equal(sourceList.Count, targetList.Count);
Assert.Equal(0, targetList.Count);
syncer2.Dispose();
sourceList.Add("Test1");
Assert.Equal(sourceList.Count, targetList.Count);
}
示例7: event_raised_when_item_is_removed_by_value
public void event_raised_when_item_is_removed_by_value()
{
var testObject = new ObservableList<int>();
var events = new List<NotifyCollectionChangedEventArgs>();
testObject.Add(1);
testObject.Add(2);
testObject.Add(3);
testObject.CollectionChanged += (source, args) => events.Add(args);
var itemRemoved = 2;
testObject.Remove(itemRemoved);
var ev = events[0];
Assert.Equal(NotifyCollectionChangedAction.Remove, ev.Action);
Assert.Equal(1, events.Count);
Assert.Equal(1, ev.OldItems.Count);
Assert.Equal(itemRemoved, ev.OldItems[0]);
Assert.Equal(1, ev.OldStartingIndex);
}
示例8: When_Remove_Then_Action_Remove_Raised
public void When_Remove_Then_Action_Remove_Raised()
{
// Arrange
var sut = new ObservableList<int>(new[] {10, 12, 18, 100});
var results = new List<NotifyCollectionChangedEventArgs>();
var expected = new[]
{
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 12, 1),
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 100, 2),
};
sut.CollectionChanged += (o, e) => results.Add(e);
// Act
sut.Remove(12);
sut.Remove(100);
// Assert
KKAssert.AreEqualByValue(expected, results);
}
示例9: Remove_FiresCollectionChanged
public void Remove_FiresCollectionChanged()
{
var list = new ObservableList<int> { 1, 2 };
list.CollectionChanged += (sender, args) =>
{
Assert.AreEqual(NotifyCollectionChangedAction.Remove, args.Action);
Assert.IsTrue(new[] { 2 }.SequenceEqual(args.OldItems.Cast<int>()));
};
list.Remove(2);
}
示例10: TestShouldModifyOriginal
public void TestShouldModifyOriginal()
{
var originalList = new List<int> {1, 2, 3};
var observableList = new ObservableList<int>(originalList);
CollectionAssert.Contains(observableList.Value, 2);
observableList.Remove(2);
CollectionAssert.DoesNotContain(originalList, 2);
}
示例11: RemoveFiresListChangedEvent
public void RemoveFiresListChangedEvent()
{
const string expectItem = "a string to remove";
const int expectIndex = 4;
var actualItem = "";
var actualIndex = -1;
var observableList = new ObservableList<string>(new[] { "A", "B", "C", "D", "E" }.AsEnumerable());
observableList.Insert(expectIndex, expectItem);
observableList.ListChanged += (s, e) =>
{
actualItem = e.Item;
actualIndex = e.Index;
};
observableList.Remove(expectItem);
Assert.AreEqual(expectItem, actualItem);
Assert.AreEqual(expectIndex, actualIndex);
}
示例12: TestIndexOf
public void TestIndexOf()
{
var list = new List<string> { "aaa", "bbb", "ccc" };
var set = new ObservableList<string>(list);
Assert.AreEqual(set.IndexOf("aaa"), 0);
Assert.AreEqual(set.IndexOf("bbb"), 1);
Assert.AreEqual(set.IndexOf("ccc"), 2);
Assert.AreEqual(set.IndexOf("ddd"), -1);
set.Add("ddd");
Assert.AreEqual(set.IndexOf("ddd"), 3);
set.Remove("bbb");
Assert.AreEqual(set.IndexOf("bbb"), -1);
Assert.AreEqual(set.IndexOf("ddd"), 2);
}
示例13: TestRemove
public void TestRemove()
{
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.Remove);
Assert.AreEqual(e.OldStartingIndex, 1);
Assert.NotNull(e.OldItems);
Assert.AreEqual(e.OldItems.Count, 1);
Assert.AreEqual(e.OldItems[0], "bbb");
collectionChangedInvoked = true;
};
set.Remove("bbb");
Assert.AreEqual(set.Count, 2);
Assert.AreEqual(set[0], "aaa");
Assert.AreEqual(set[1], "ccc");
Assert.True(propertyChangedInvoked);
Assert.True(collectionChangedInvoked);
}
示例14: ChangeCollection
public void ChangeCollection()
{
_handle = new EventWaitHandle(false, EventResetMode.ManualReset);
var test = new ObservableList<string>();
test.Changed += List_Changed;
test.Add("myValue");
Assert.IsTrue(_handle.WaitOne(10), "Add() is not recognized as a change");
_handle.Reset();
test[0] = "newValue";
Assert.IsTrue(_handle.WaitOne(10), "this[] is not recognized as a change");
_handle.Reset();
test.RemoveAt(0);
Assert.IsTrue(_handle.WaitOne(10), "RemoveAt() is not recognized as a change");
_handle.Reset();
test.Add("myValue");
Assert.IsTrue(_handle.WaitOne(10), "Add() is not recognized as a change");
_handle.Reset();
test.Remove("myValue");
Assert.IsTrue(_handle.WaitOne(10), "Remove() is not recognized as a change");
}