当前位置: 首页>>代码示例>>C#>>正文


C# ObservableCollection.Remove方法代码示例

本文整理汇总了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);
        }
开发者ID:fs7744,项目名称:ObjectValidator,代码行数:27,代码来源:ObservableCollection_Test.cs

示例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.");
        }
开发者ID:Zim-Code,项目名称:ViewModels,代码行数:28,代码来源:Test_LabeledViewModelCollection.cs

示例3: InitValues

        private void InitValues()
        {
            Values = new ObservableCollection<IOrderable>(_values);

            foreach (var item in SelectedValues)
            {
                if (Values.Contains(item))
                    Values.Remove(item);
            }
        }
开发者ID:hpbaotho,项目名称:sambapos,代码行数:10,代码来源:ValueChooserForm.xaml.cs

示例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);
            }
        }
开发者ID:EvilInteractive,项目名称:happy-engine,代码行数:14,代码来源:MainWindow.xaml.cs

示例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);
        }
开发者ID:ismell,项目名称:Continuous-LINQ,代码行数:18,代码来源:SortTest.cs

示例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]);
        }
开发者ID:ssethi,项目名称:TestFrameworks,代码行数:18,代码来源:ViewsCollectionFixture.cs

示例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;
            }
        }
开发者ID:nikolauska,项目名称:StringForge,代码行数:31,代码来源:StringTableEditorViewModel.cs

示例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;
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:56,代码来源:ReadOnlyObservableCollection_EventsTests.cs

示例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;
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:65,代码来源:ObservableCollection_MethodsTest.cs

示例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);
     }
 }
开发者ID:justdude,项目名称:OrdersManager,代码行数:9,代码来源:Node.cs

示例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);
         }
     }
 }
开发者ID:nullkuhl,项目名称:fsu-dev,代码行数:15,代码来源:MainWindow2.xaml.cs

示例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;
        }
开发者ID:BBuchholz,项目名称:NineWorldsDeep,代码行数:18,代码来源:SynergyListManagementWindow.xaml.cs

示例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;
                }
            }
        }
开发者ID:BBuchholz,项目名称:NineWorldsDeep,代码行数:24,代码来源:SynergyListManagementWindow.xaml.cs

示例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);
        }
开发者ID:selvendiranj,项目名称:compositewpf-copy,代码行数:13,代码来源:ViewsCollectionFixture.cs

示例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);
 }
开发者ID:serious198706,项目名称:ListBox2Demo,代码行数:6,代码来源:MainPage.xaml.cs


注:本文中的System.Collections.ObservableCollection.Remove方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。