Example #1
0
        public void Move_OneItem_ItemIsMovedToNewIndex()
        {
            var collectionChangedEventArgsList = new List <NotifyCollectionChangedEventArgs>();

            var collection = new ExtendedObservableCollection <int> {
                123, 456, 789
            };

            collection.CollectionChanged += (_, args) => collectionChangedEventArgsList.Add(args);

            collection.Move(1, 2);

            Assert.Equal(new[] { 123, 789, 456 }, collection);

            Assert.Single(collectionChangedEventArgsList);
            Assert.Equal(NotifyCollectionChangedAction.Move, collectionChangedEventArgsList[0].Action);

            Assert.NotNull(collectionChangedEventArgsList[0].NewItems);
            Assert.Single(collectionChangedEventArgsList[0].NewItems);

            Assert.Equal(456, collectionChangedEventArgsList[0].NewItems[0]);

            Assert.NotNull(collectionChangedEventArgsList[0].OldItems);
            Assert.Single(collectionChangedEventArgsList[0].OldItems);

            Assert.Equal(456, collectionChangedEventArgsList[0].OldItems[0]);
            Assert.Equal(1, collectionChangedEventArgsList[0].OldStartingIndex);
            Assert.Equal(2, collectionChangedEventArgsList[0].NewStartingIndex);
        }
Example #2
0
        public void Move_TwoItems_ItemsAreMovedToNewIndex()
        {
            var collectionChangedEventArgsList = new List <NotifyCollectionChangedEventArgs>();

            var collection = new ExtendedObservableCollection <int> {
                123, 456, 789, 135, 246
            };

            collection.CollectionChanged += (_, args) => collectionChangedEventArgsList.Add(args);

            collection.Move(1, 2);
            collection.Move(4, 0);

            Assert.Equal(new[] { 246, 123, 789, 456, 135 }, collection);

            Assert.Equal(2, collectionChangedEventArgsList.Count);

            Assert.Equal(NotifyCollectionChangedAction.Move, collectionChangedEventArgsList[0].Action);

            Assert.NotNull(collectionChangedEventArgsList[0].NewItems);
            Assert.Single(collectionChangedEventArgsList[0].NewItems);

            Assert.Equal(456, collectionChangedEventArgsList[0].NewItems[0]);

            Assert.NotNull(collectionChangedEventArgsList[0].OldItems);
            Assert.Single(collectionChangedEventArgsList[0].OldItems);

            Assert.Equal(456, collectionChangedEventArgsList[0].OldItems[0]);
            Assert.Equal(1, collectionChangedEventArgsList[0].OldStartingIndex);
            Assert.Equal(2, collectionChangedEventArgsList[0].NewStartingIndex);

            Assert.Equal(NotifyCollectionChangedAction.Move, collectionChangedEventArgsList[1].Action);

            Assert.NotNull(collectionChangedEventArgsList[1].NewItems);
            Assert.Single(collectionChangedEventArgsList[1].NewItems);

            Assert.Equal(246, collectionChangedEventArgsList[1].NewItems[0]);

            Assert.NotNull(collectionChangedEventArgsList[1].OldItems);
            Assert.Single(collectionChangedEventArgsList[1].OldItems);

            Assert.Equal(246, collectionChangedEventArgsList[1].OldItems[0]);
            Assert.Equal(4, collectionChangedEventArgsList[1].OldStartingIndex);
            Assert.Equal(0, collectionChangedEventArgsList[1].NewStartingIndex);
        }
Example #3
0
        public static void Main(string[] args)
        {
            var collection = new ExtendedObservableCollection <string>();

            collection.CollectionChanged += Collection_CollectionChanged;

            collection.Add("item1");
            collection.AddRange(new string[] { "item2", "item3", "item4" });
            collection[0] = "item1 mk2";
            collection.Move(0, 3);
            collection.Remove("item3");
            collection.Clear();
        }
Example #4
0
        public static void Sort <T>(this ExtendedObservableCollection <T> collection) where T : IComparable <T>
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            for (var startIndex = 0; startIndex < collection.Count - 1; startIndex += 1)
            {
                var indexOfSmallestItem = startIndex;
                for (var i = startIndex + 1; i < collection.Count; i += 1)
                {
                    if (collection[i].CompareTo(collection[indexOfSmallestItem]) < 0)
                    {
                        indexOfSmallestItem = i;
                    }
                }
                if (indexOfSmallestItem != startIndex)
                {
                    collection.Move(indexOfSmallestItem, startIndex);
                }
            }
        }