本文整理汇总了C#中System.Collections.ObservableCollection.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableCollection.Remove方法的具体用法?C# ObservableCollection.Remove怎么用?C# ObservableCollection.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ObservableCollection
的用法示例。
在下文中一共展示了ObservableCollection.Remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_ObservableCollection_Remove
public void Test_ObservableCollection_Remove()
{
var list = new ObservableCollection<int>() { 4, 5, 6 };
Assert.Equal(3, list.Count);
list.Remove(4);
Assert.Equal(2, list.Count);
Assert.DoesNotContain(4, list);
var collection = list as ICollection<int>;
Assert.NotNull(collection);
Assert.True(collection.Remove(5));
Assert.Equal(1, collection.Count);
Assert.DoesNotContain(5, list);
list = new ObservableCollection<int>() { 4, 5, 6 };
list.CollectionChanged += (o, e) =>
{
Assert.Same(list, o);
Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
Assert.Null(e.NewItems);
Assert.NotNull(e.OldItems);
Assert.Equal(1, e.OldItems.Count);
Assert.Equal(5, e.OldItems[0]);
};
list.RemoveAt(1);
Assert.Equal(2, list.Count);
Assert.DoesNotContain(5, list);
}
示例2: TestCollectionSync
public void TestCollectionSync()
{
string item0 = "Item0";
string item1 = "Item1";
string item2 = "Item2";
string item3 = "Item3";
ObservableCollection<string> collection = new ObservableCollection<string>();
HelperLabeledViewModelCollection viewModel = new HelperLabeledViewModelCollection(null, collection, o => o);
collection.Add(item0);
collection.Add(item1);
collection.Add(item3);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Add did not work.");
collection.Insert(2, item2);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Insert did not work.");
collection.Remove(item3);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Remove did not work.");
collection.Move(0, 1);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Move did not work.");
collection.Clear();
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Clear did not work.");
}
示例3: InitValues
private void InitValues()
{
Values = new ObservableCollection<IOrderable>(_values);
foreach (var item in SelectedValues)
{
if (Values.Contains(item))
Values.Remove(item);
}
}
示例4: moveModelItemFromLBtoL
private void moveModelItemFromLBtoL(IList items, ObservableCollection<ModelItem> from, ObservableCollection<ModelItem> to)
{
List<ModelItem> tempList = new List<ModelItem>(items.Count);
foreach (ModelItem item in items)
{
to.Add(item);
tempList.Add(item);
}
foreach (ModelItem item in tempList)
{
from.Remove(item);
}
}
示例5: RemoveFromSource_LastItemInCollection_CountIsZeroAndNoExceptionThrown
public void RemoveFromSource_LastItemInCollection_CountIsZeroAndNoExceptionThrown()
{
var sourceWithTwoItems = new ObservableCollection<Person>();
var personOne = new Person("Bob", 10);
var personTwo = new Person("Jim", 20);
ReadOnlyContinuousCollection<string> output =
from person in sourceWithTwoItems
where person.Age <= 20
orderby person.Name
select person.Name;
sourceWithTwoItems.Add(personOne);
sourceWithTwoItems.Add(personTwo);
sourceWithTwoItems.Remove(personOne);
//Assert.AreEqual(_source.Count, output.Count);
}
示例6: CollectionChangedPassesWrappedItemInArgumentsWhenAdding
public void CollectionChangedPassesWrappedItemInArgumentsWhenAdding()
{
var originalCollection = new ObservableCollection<ItemMetadata>();
var filteredInObject = new ItemMetadata(new object());
originalCollection.Add(filteredInObject);
IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => true);
IList oldItemsPassed = null;
viewsCollection.CollectionChanged += (s, e) =>
{
oldItemsPassed = e.OldItems;
};
originalCollection.Remove(filteredInObject);
Assert.IsNotNull(oldItemsPassed);
Assert.AreEqual(1, oldItemsPassed.Count);
Assert.AreSame(filteredInObject.Item, oldItemsPassed[0]);
}
示例7: UnloadProjectCommandExecute
/// <summary>
/// Execute an unload project command on selected note
/// </summary>
private void UnloadProjectCommandExecute()
{
if (this.SelectedNode != null && this.SelectedNode.GetType() == typeof(Project))
{
var dlg = MessageBox.Show("Would you like to save the project before unloading it?", "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
var selectedProject = (Project)this.SelectedNode;
var projectsCollection = new ObservableCollection<Project>(this.Projects);
// canceling will return
switch (dlg)
{
case MessageBoxResult.Cancel:
return;
case MessageBoxResult.Yes:
QuickSaveProject(selectedProject);
break;
}
// remove the selected project
projectsCollection.Remove(selectedProject);
// force ui refresh
this.Projects = projectsCollection;
}
}
示例8: RemoveItemTest
/// <summary>
/// Given a collection, index and item to remove, will try to remove that item
/// from the index. If the item has duplicates, will verify that only the first
/// instance was removed.
/// </summary>
public void RemoveItemTest(ReadOnlyObservableCollection<string> readOnlyCol, ObservableCollection<string> collection,
int itemIndex, string itemToRemove, bool isSuccessfulRemove, bool hasDuplicates)
{
INotifyPropertyChanged readOnlyPropertyChanged = readOnlyCol;
readOnlyPropertyChanged.PropertyChanged += Collection_PropertyChanged;
_expectedPropertyChanged = new[]
{
new PropertyNameExpected(COUNT),
new PropertyNameExpected(ITEMARRAY)
};
INotifyCollectionChanged readOnlyCollectionChange = readOnlyCol;
readOnlyCollectionChange.CollectionChanged += Collection_CollectionChanged;
if (isSuccessfulRemove)
_expectedCollectionChangedFired++;
_expectedAction = NotifyCollectionChangedAction.Remove;
_expectedNewItems = null;
_expectedNewStartingIndex = -1;
_expectedOldItems = new string[] { itemToRemove };
_expectedOldStartingIndex = itemIndex;
int expectedCount = isSuccessfulRemove ? collection.Count - 1 : collection.Count;
bool removedItem = collection.Remove(itemToRemove);
Assert.Equal(expectedCount, readOnlyCol.Count);
Assert.Equal(_expectedCollectionChangedFired, _numCollectionChangedFired);
if (isSuccessfulRemove)
{
foreach (var item in _expectedPropertyChanged)
Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since an item was removed");
Assert.True(removedItem, "Should have been successful in removing the item.");
}
else
{
foreach (var item in _expectedPropertyChanged)
Assert.False(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since no items were removed.");
Assert.False(removedItem, "Should not have been successful in removing the item.");
}
if (hasDuplicates)
return;
Assert.DoesNotContain(itemToRemove, collection);
readOnlyCollectionChange.CollectionChanged -= Collection_CollectionChanged;
readOnlyPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
}
示例9: RemoveItemTest
/// <summary>
/// Given a collection, index and item to remove, will try to remove that item
/// from the index. If the item has duplicates, will verify that only the first
/// instance was removed.
/// </summary>
public void RemoveItemTest(ObservableCollection<string> collection, int itemIndex, string itemToRemove, bool isSuccessfulRemove, bool hasDuplicates)
{
INotifyPropertyChanged collectionPropertyChanged = collection;
collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
_expectedPropertyChanged = new[]
{
new PropertyNameExpected(COUNT),
new PropertyNameExpected(ITEMARRAY)
};
collection.CollectionChanged += Collection_CollectionChanged;
if (isSuccessfulRemove)
ExpectedCollectionChangedFired++;
ExpectedAction = NotifyCollectionChangedAction.Remove;
ExpectedNewItems = null;
ExpectedNewStartingIndex = -1;
ExpectedOldItems = new string[] { itemToRemove };
ExpectedOldStartingIndex = itemIndex;
int expectedCount = isSuccessfulRemove ? collection.Count - 1 : collection.Count;
bool removedItem = collection.Remove(itemToRemove);
Assert.Equal(expectedCount, collection.Count);
Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);
if (isSuccessfulRemove)
{
foreach (var item in _expectedPropertyChanged)
Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were items removed.");
Assert.True(removedItem, "Should have been successful in removing the item.");
}
else
{
foreach (var item in _expectedPropertyChanged)
Assert.False(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were no items removed.");
Assert.False(removedItem, "Should not have been successful in removing the item.");
}
if (hasDuplicates)
return;
// ensuring that the item is not in the collection.
for (int i = 0; i < collection.Count; i++)
{
if (itemToRemove == collection[i])
{
string itemsInCollection = "";
foreach (var item in collection)
itemsInCollection += item + ", ";
Assert.True(false, "Found item (" + itemToRemove + ") that should not be in the collection because we tried to remove it. Collection: " + itemsInCollection);
}
}
collection.CollectionChanged -= Collection_CollectionChanged;
collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
}
示例10: RemoveRecursive
public static void RemoveRecursive(ObservableCollection<Node> nodes, Node target)
{
bool result = nodes.Remove(target);
if (!result)
{
foreach(var node in nodes)
RemoveSelected(node,target);
}
}
示例11: FindAndDeleteDirectory
void FindAndDeleteDirectory(DirectoryInfo d, ObservableCollection<AppFolder> context)
{
foreach (AppFolder item in context)
{
if (item.FullPath == d.FullName)
{
context.Remove(item);
break;
}
if (item.SubFolders.Count > 0)
{
FindAndDeleteDirectory(d, item.SubFolders);
}
}
}
示例12: MoveAll
private void MoveAll(ObservableCollection<SynergyList> from,
ObservableCollection<SynergyList> to)
{
List<SynergyList> toBeMoved = new List<SynergyList>();
foreach (SynergyList lst in from)
{
toBeMoved.Add(lst);
}
foreach (SynergyList lst in toBeMoved)
{
from.Remove(lst);
to.Add(lst);
}
IsChanged = true;
}
示例13: MoveSelected
private void MoveSelected(ListView from,
ObservableCollection<SynergyList> fromCol,
ObservableCollection<SynergyList> toCol)
{
IList items = (IList)from.SelectedItems;
var selectedLists = items.Cast<SynergyList>();
List<SynergyList> toBeMoved = new List<SynergyList>();
foreach (SynergyList selected in selectedLists)
{
toBeMoved.Add(selected);
}
foreach(SynergyList selected in toBeMoved)
{
if (selected != null)
{
fromCol.Remove(selected);
toCol.Add(selected);
IsChanged = true;
}
}
}
示例14: DoesNotRaiseCollectionChangedWhenAddingOrRemovingFilteredOutObject
public void DoesNotRaiseCollectionChangedWhenAddingOrRemovingFilteredOutObject()
{
var originalCollection = new ObservableCollection<ItemMetadata>();
IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
bool collectionChanged = false;
viewsCollection.CollectionChanged += (s, e) => collectionChanged = true;
var filteredOutObject = new ItemMetadata(new object()) { IsActive = false };
originalCollection.Add(filteredOutObject);
originalCollection.Remove(filteredOutObject);
Assert.IsFalse(collectionChanged);
}
示例15: delete_one
private void delete_one(ObservableCollection<Todo> todos)
{
if (ScheduledActionService.Find((App.Current as App).todo_delete.ReminderName) != null)
ScheduledActionService.Remove((App.Current as App).todo_delete.ReminderName);
todos.Remove((App.Current as App).todo_delete);
}