コード例 #1
0
        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
0
        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
0
        private void InitValues()
        {
            Values = new ObservableCollection<IOrderable>(_values);

            foreach (var item in SelectedValues)
            {
                if (Values.Contains(item))
                    Values.Remove(item);
            }
        }
コード例 #4
0
        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
0
ファイル: SortTest.cs プロジェクト: ismell/Continuous-LINQ
        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
0
        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
0
        /// <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
0
        /// <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
0
        /// <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
0
ファイル: Node.cs プロジェクト: justdude/OrdersManager
 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
0
ファイル: MainWindow2.xaml.cs プロジェクト: nullkuhl/fsu-dev
 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
0
        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
0
        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
0
        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
0
 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);
 }
コード例 #16
0
        public void RaisesCollectionChangedWithAddAndRemoveWhenFilteredCollectionChanges()
        {
            var originalCollection = new ObservableCollection<ItemMetadata>();
            IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
            bool addedToCollection = false;
            bool removedFromCollection = false;
            viewsCollection.CollectionChanged += (s, e) =>
                                                     {
                                                         if (e.Action == NotifyCollectionChangedAction.Add)
                                                         {
                                                             addedToCollection = true;
                                                         }
                                                         else if (e.Action == NotifyCollectionChangedAction.Remove)
                                                         {
                                                             removedFromCollection = true;
                                                         }
                                                     };
            var filteredInObject = new ItemMetadata(new object()) { IsActive = true };

            originalCollection.Add(filteredInObject);

            Assert.IsTrue(addedToCollection);
            Assert.IsFalse(removedFromCollection);

            originalCollection.Remove(filteredInObject);

            Assert.IsTrue(removedFromCollection);
        }
コード例 #17
0
        private void delete_all(ObservableCollection<Todo> todos)
        {
            if (MessageBox.Show("您确定要清除所有已完成项吗?", "友情提示", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
            {
                return;
            }

            // 如果边遍历边删除的话,就会崩溃
            // 所以要把所有需要删除的todo都收集起来,再挨个删除
            List<Todo> toDelete = new List<Todo>();

            foreach (object item in todos)
            {
                Todo todo = item as Todo;
                if (todo.IsCompleted)
                {
                    toDelete.Add(todo);
                }
            }

            while (toDelete.Count > 0)
            {
                // 删除时,注销提醒事件,再删除todo
                if (ScheduledActionService.Find(toDelete[0].ReminderName) != null)
                    ScheduledActionService.Remove(toDelete[0].ReminderName);

                todos.Remove(toDelete[0]);
                toDelete.RemoveAt(0);
            }
        }
コード例 #18
0
 // Removes the wizard pages of the passed-in search providers from the search tool's configuration wizard
 private void removeWizardPages(IEnumerable providers, ObservableCollection<WizardPage> pages)
 {
     foreach (ISearchProvider provider in providers)
     {
         ISupportsWizardConfiguration wizardInfo = provider as ISupportsWizardConfiguration;
         if (wizardInfo != null)
         {
             foreach (WizardPage page in wizardInfo.Pages)
             {
                 if (pages.Contains(page))
                     pages.Remove(page);
             }
         }
     }
 }
コード例 #19
0
ファイル: GroupByTest.cs プロジェクト: FrederikP/NMF
        public void GroupBy_ObservableSourceItemRemoved_Update()
        {
            var update = false;
            ICollection<Dummy<string>> coll = new ObservableCollection<Dummy<string>>();
            var dummy = new Dummy<string>("42");
            coll.Add(dummy);

            var test = coll.WithUpdates().GroupBy(d => d.Item);

            test.CollectionChanged += (o, e) =>
            {
                Assert.IsTrue(ContainsGroup(e.OldItems, "42"));
                Assert.AreEqual(1, e.OldItems.Count);
                update = true;
            };

            Assert.IsTrue(Sys.Any(test, group => group.Key == "42"));
            Assert.IsFalse(update);

            coll.Remove(dummy);

            Assert.IsFalse(test.Any());
            Assert.IsTrue(update);
        }