CollectionChanged

Namespace: ReactiveProperty
We found 10 examples in language CSharp for this search. You will see 53 fragments of code.


        #endregion

        #region Public Methods

        /// <summary>
        /// Adds an item to the end of the list
        /// </summary>
        /// <param name="item">The object to be added to the end of the collection. The value can be null for reference types.</param>
        public new void Add(CalendarItem item)
        {
            base.Add(item); CollectionChanged();
        }

        /// <summary>
        /// Adds the items of the specified collection to the end of the list.
        /// </summary>
        /// <param name="items">The items whose elements should be added to the end of the collection. The collection itself cannont be null, but it can contain elements that are null.</param>
        public new void AddRange(IEnumerable<CalendarItem> items)
        {
            base.AddRange(items); CollectionChanged();
        }

        /// <summary>
        /// Removes all elements from the collection.
        /// </summary>
        public new void Clear()
        {
            base.Clear(); CollectionChanged();
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        public new void Insert(int index, CalendarItem item) 
        {
            base.Insert(index, item); CollectionChanged();
        }

        /// <summary>
        /// Inserts the items of a collection into this collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
        /// <param name="items"></param>
        public new void InsertRange(int index, IEnumerable<CalendarItem> items) 
        {
            base.InsertRange(index, items); CollectionChanged();
        }

        /// <summary>
        /// Removes the item at the specified index of the collection
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        /// <returns></returns>
        public new void RemoveAt(int index)
        {
            base.RemoveAt(index); CollectionChanged();
        }



        #endregion

        #region Public Methods

        /// <summary>
        /// Adds an item to the end of the list
        /// </summary>
        /// <param name="item">The object to be added to the end of the collection. The value can be null for reference types.</param>
        public new void Add(CalendarItem item)
        {
            base.Add(item); CollectionChanged();
        }

        /// <summary>
        /// Adds the items of the specified collection to the end of the list.
        /// </summary>
        /// <param name="items">The items whose elements should be added to the end of the collection. The collection itself cannont be null, but it can contain elements that are null.</param>
        public new void AddRange(IEnumerable<CalendarItem> items)
        {
            base.AddRange(items); CollectionChanged();
        }

        /// <summary>
        /// Removes all elements from the collection.
        /// </summary>
        public new void Clear()
        {
            base.Clear(); CollectionChanged();
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        public new void Insert(int index, CalendarItem item) 
        {
            base.Insert(index, item); CollectionChanged();
        }

        /// <summary>
        /// Inserts the items of a collection into this collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
        /// <param name="items"></param>
        public new void InsertRange(int index, IEnumerable<CalendarItem> items) 
        {
            base.InsertRange(index, items); CollectionChanged();
        }

        /// <summary>
        /// Removes the item at the specified index of the collection
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        /// <returns></returns>
        public new void RemoveAt(int index)
        {
            base.RemoveAt(index); CollectionChanged();
        }


        private void DoClear()
        {
            this.sync.AcquireWriterLock(Timeout.Infinite);
            this._collection.Clear();
            if (this.CollectionChanged != null)
            {
                this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
            this.sync.ReleaseWriterLock();
        }

        private void DoInsert(int index, T item)
        {
            this.sync.AcquireWriterLock(Timeout.Infinite);
            this._collection.Insert(index, item);
            if (this.CollectionChanged != null)
            {
                this.CollectionChanged(
                    this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
            }
            this.sync.ReleaseWriterLock();
        }


        #endregion

        #region IList and ICollection implementation

        public void Add(T item)
        {
            list.Add(item);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
        }

        public void Insert(int index, T item)
        {
            list.Insert(index, item);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
        }

        public void RemoveAt(int index)
        {
            var old = list[index];
            list.RemoveAt(index);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, old, index));
        }

        public void Clear()
        {
            list.Clear();
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool Remove(T item)
        {
            bool r = list.Remove(item);
            if (r)
                CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
            return r;
        }


        public void Insert(int index, BindableObject item)
        {
            _items.Insert(index, item);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
        }

        public void RemoveAt(int index)
        {
            var oldItem = this[index];
            _items.RemoveAt(index);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, index));
        }

        public void Add(BindableObject item)
        {
            _items.Add(item);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, Count - 1));
        }

        public void Clear()
        {
            _items.Clear();
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool Remove(BindableObject item)
        {
            var oldIndex = IndexOf(item);
            if (_items.Remove(item))
            {
                CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, oldIndex));
                return true;
            }
            return false;
        }



        #endregion

        #region Public Methods

        /// <summary>
        /// Adds an item to the end of the list
        /// </summary>
        /// <param name="item">The object to be added to the end of the collection. The value can be null for reference types.</param>
        public new void Add(CalendarItem item)
        {
            base.Add(item); CollectionChanged();
        }

        /// <summary>
        /// Adds the items of the specified collection to the end of the list.
        /// </summary>
        /// <param name="items">The items whose elements should be added to the end of the collection. The collection itself cannont be null, but it can contain elements that are null.</param>
        public new void AddRange(IEnumerable<CalendarItem> items)
        {
            base.AddRange(items); CollectionChanged();
        }

        /// <summary>
        /// Removes all elements from the collection.
        /// </summary>
        public new void Clear()
        {
            base.Clear(); CollectionChanged();
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        public new void Insert(int index, CalendarItem item)
        {
            base.Insert(index, item); CollectionChanged();
        }

        /// <summary>
        /// Inserts the items of a collection into this collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
        /// <param name="items"></param>
        public new void InsertRange(int index, IEnumerable<CalendarItem> items)
        {
            base.InsertRange(index, items); CollectionChanged();
        }

        /// <summary>
        /// Removes the item at the specified index of the collection
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        /// <returns></returns>
        public new void RemoveAt(int index)
        {
            base.RemoveAt(index); CollectionChanged();
        }

		
		void CreatePages()
		{
			pages = new Pages();
		}
		
		[Test]
		public void Pages_TotalItemsChanged_PagesUpdatedAfterChangeInTotalItems()
		{
			CreatePages();
			pages.PageSize = 2;
			pages.TotalItems = 2;
			
			int oldPageCount = pages.Count;
			pages.TotalItems = 3;
			int newPageCount = pages.Count;
			
			Assert.AreEqual(2, newPageCount);
			Assert.AreEqual(1, oldPageCount);
		}
		
		[Test]
		public void Pages_SelectedPageNumberChanged_PagesUpdatedAfterChange()
		{
			CreatePages();
			pages.PageSize = 1;
			pages.TotalItems = 2;
			pages.SelectedPageNumber = 1;
			
			pages.SelectedPageNumber = 2;
			
			Page[] expectedPages = new Page[] {
				new Page() { Number = 1 },
				new Page() { Number = 2, IsSelected = true }
			};
			
			PageCollectionAssert.AreEqual(expectedPages, pages);
		}
		
		[Test]
		public void Pages_PageSizeChanged_PagesUpdatedAfterChange()
		{
			CreatePages();
			pages.PageSize = 2;
			pages.SelectedPageNumber = 1;
			pages.TotalItems = 2;
			
			pages.PageSize = 1;
			
			Page[] expectedPages = new Page[] {
				new Page() { Number = 1, IsSelected = true },
				new Page() { Number = 2 }
			};
			
			PageCollectionAssert.AreEqual(expectedPages, pages);
		}
		
		[Test]
		public void Pages_MaximumSelectablePagesChanged_PagesUpdatedAfterChange()
		{
			CreatePages();
			pages.MaximumSelectablePages = 5;
			pages.PageSize = 2;
			pages.SelectedPageNumber = 1;
			pages.TotalItems = 10;
			
			pages.MaximumSelectablePages = 2;
			
			Page[] expectedPages = new Page[] {
				new Page() { Number = 1, IsSelected = true },
				new Page() { Number = 2 }
			};
			
			PageCollectionAssert.AreEqual(expectedPages, pages);
		}
		
		[Test]
		public void Pages_MaximumSelectablePagesSetToSameValue_PagesCollectionNotChanged()
		{
			CreatePages();
			pages.TotalItems = 1;
			pages.MaximumSelectablePages = 3;
			
			bool collectionChanged = false;
			pages.CollectionChanged += (sender, e) => collectionChanged = true;
			
			pages.MaximumSelectablePages = 3;
			
			Assert.IsFalse(collectionChanged);
		}


		public void Add(T item) => 
			collectionChanged(master, new(NotifyCollectionChangedAction.Add, new[] { item }));

		public void Clear() => 
			collectionChanged(master, new(Reset));

		public bool PreRemove(T item)
		{
			removedIndex = getIndex(item);
			return removedIndex != -1;
		}

		public bool Remove(T item)
		{
			if (removedIndex == -1)
				return false;
			collectionChanged(master, new(
				NotifyCollectionChangedAction.Remove,
				new[] { item }, removedIndex));
			return true;
		}

		public void Bind(object target) => 
			master = target;


    [ConditionalFact]
    public void Can_construct()
    {
        Assert.Same(
            new HashSet<int>().Comparer,
            new ObservableHashSet<int>().Comparer);

        Assert.Same(
            LegacyReferenceEqualityComparer.Instance,
            new ObservableHashSet<object>(LegacyReferenceEqualityComparer.Instance).Comparer);

        var testData1 = CreateTestData();

        var rh1 = new HashSet<int>(testData1);
        var ohs1 = new ObservableHashSet<int>(testData1);
        Assert.Equal(rh1.OrderBy(i => i), ohs1.OrderBy(i => i));
        Assert.Same(rh1.Comparer, ohs1.Comparer);

        var testData2 = CreateTestData().Cast<object>();

        var rh2 = new HashSet<object>(testData2, LegacyReferenceEqualityComparer.Instance);
        var ohs2 = new ObservableHashSet<object>(testData2, LegacyReferenceEqualityComparer.Instance);
        Assert.Equal(rh2.OrderBy(i => i), ohs2.OrderBy(i => i));
        Assert.Same(rh2.Comparer, ohs2.Comparer);
    }

    [ConditionalFact]
    public void Can_add()
    {
        var hashSet = new ObservableHashSet<string>();
        var countChanging = 0;
        var countChanged = 0;
        var collectionChanged = 0;
        var currentCount = 0;
        var countChange = 1;
        var adding = Array.Empty<string>();

        hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
        hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
        hashSet.CollectionChanged += (s, a) =>
        {
            Assert.Equal(NotifyCollectionChangedAction.Add, a.Action);
            Assert.Null(a.OldItems);
            Assert.Equal(adding, a.NewItems.OfType<string>());
            collectionChanged++;
        };

        adding = new[] { "Palmer" };
        Assert.True(hashSet.Add("Palmer"));

        Assert.Equal(1, countChanging);
        Assert.Equal(1, countChanged);
        Assert.Equal(1, collectionChanged);
        Assert.Equal(new[] { "Palmer" }, hashSet);

        adding = new[] { "Carmack" };
        Assert.True(hashSet.Add("Carmack"));

        Assert.Equal(2, countChanging);
        Assert.Equal(2, countChanged);
        Assert.Equal(2, collectionChanged);
        Assert.Equal(new[] { "Carmack", "Palmer" }, hashSet.OrderBy(i => i));

        Assert.False(hashSet.Add("Palmer"));

        Assert.Equal(2, countChanging);
        Assert.Equal(2, countChanged);
        Assert.Equal(2, collectionChanged);
        Assert.Equal(new[] { "Carmack", "Palmer" }, hashSet.OrderBy(i => i));
    }

    [ConditionalFact]
    public void Can_clear()
    {
        var testData = new HashSet<int>(CreateTestData());

        var hashSet = new ObservableHashSet<int>(testData);
        var countChanging = 0;
        var countChanged = 0;
        var collectionChanged = 0;
        var currentCount = testData.Count;
        var countChange = -testData.Count;

        hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
        hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
        hashSet.CollectionChanged += (s, a) =>
        {
            Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
            Assert.Equal(testData.OrderBy(i => i), a.OldItems.OfType<int>().OrderBy(i => i));
            Assert.Empty(a.NewItems);
            collectionChanged++;
        };

        hashSet.Clear();

        Assert.Equal(testData.Count == 0 ? 0 : 1, countChanging);
        Assert.Equal(testData.Count == 0 ? 0 : 1, countChanged);
        Assert.Equal(testData.Count == 0 ? 0 : 1, collectionChanged);
        Assert.Empty(hashSet);

        hashSet.Clear();

        Assert.Equal(testData.Count == 0 ? 0 : 1, countChanging);
        Assert.Equal(testData.Count == 0 ? 0 : 1, countChanged);
        Assert.Equal(testData.Count == 0 ? 0 : 1, collectionChanged);
        Assert.Empty(hashSet);
    }

    [ConditionalFact]
    public void Contains_works()
    {
        var testData = CreateTestData();
        var hashSet = new ObservableHashSet<int>(testData);

        foreach (var item in testData)
        {
            Assert.Contains(item, hashSet);
        }

        foreach (var item in CreateTestData(1000, 10000).Except(testData))
        {
            Assert.DoesNotContain(item, hashSet);
        }
    }

    [ConditionalFact]
    public void Can_copy_to_array()
    {
        var testData = CreateTestData();
        var orderedDistinct = testData.Distinct().OrderBy(i => i).ToList();

        var hashSet = new ObservableHashSet<int>(testData);

        Assert.Equal(orderedDistinct.Count, hashSet.Count);

        var array = new int[hashSet.Count];
        hashSet.CopyTo(array);

        Assert.Equal(orderedDistinct, array.OrderBy(i => i));

        array = new int[hashSet.Count + 100];
        hashSet.CopyTo(array, 100);

        Assert.Equal(orderedDistinct, array.Skip(100).OrderBy(i => i));

        var toTake = Math.Min(10, hashSet.Count);
        array = new int[100 + toTake];
        hashSet.CopyTo(array, 100, toTake);

        foreach (var value in array.Skip(100).Take(toTake))
        {
            Assert.Contains(value, hashSet);
        }
    }

    [ConditionalFact]
    public void Can_remove()
    {
        var hashSet = new ObservableHashSet<string> { "Palmer", "Carmack" };
        var countChanging = 0;
        var countChanged = 0;
        var collectionChanged = 0;
        var currentCount = 2;
        var countChange = -1;
        var removing = Array.Empty<string>();

        hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
        hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
        hashSet.CollectionChanged += (s, a) =>
        {
            Assert.Equal(NotifyCollectionChangedAction.Remove, a.Action);
            Assert.Equal(removing, a.OldItems.OfType<string>());
            Assert.Null(a.NewItems);
            collectionChanged++;
        };

        removing = new[] { "Palmer" };
        Assert.True(hashSet.Remove("Palmer"));

        Assert.Equal(1, countChanging);
        Assert.Equal(1, countChanged);
        Assert.Equal(1, collectionChanged);
        Assert.Equal(new[] { "Carmack" }, hashSet);

        removing = new[] { "Carmack" };
        Assert.True(hashSet.Remove("Carmack"));

        Assert.Equal(2, countChanging);
        Assert.Equal(2, countChanged);
        Assert.Equal(2, collectionChanged);
        Assert.Empty(hashSet);

        Assert.False(hashSet.Remove("Palmer"));

        Assert.Equal(2, countChanging);
        Assert.Equal(2, countChanged);
        Assert.Equal(2, collectionChanged);
        Assert.Empty(hashSet);
    }


        public int IndexOf(T item)
        {
            return innerList.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            innerList.Insert(index, item);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, NotifyCollectionChangedEventArgs.ItemAdded(item, index));
            }
        }

        public void RemoveAt(int index)
        {
            var item = innerList[index];

            innerList.RemoveAt(index);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, NotifyCollectionChangedEventArgs.ItemRemoved(item, index));
            }
        }

        public void Add(T item)
        {
            var newIndex = innerList.Count;

            innerList.Add(item);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, NotifyCollectionChangedEventArgs.ItemAdded(item, newIndex));
            }
        }

        public void Clear()
        {
            var oldItems = innerList.Cast<object>().ToArray();

            innerList.Clear();

            if (CollectionChanged != null)
            {
                CollectionChanged(this, NotifyCollectionChangedEventArgs.Reset(oldItems));
            }
        }

        public bool Contains(T item)
        {
            return innerList.Contains(item);
        }

Reactive.Bindings.CollectionChanged<T> : Object

Constructors :

public CollectionChanged()

Methods :

public static CollectionChanged get_Reset()
public static CollectionChanged ResetWithSource(IEnumerable<T> source = )
public static CollectionChanged Remove(Int32 index = , T value = )
public static CollectionChanged Remove(Int32 index = , IEnumerable<T> values = )
public static CollectionChanged Add(Int32 index = , T value = )
public static CollectionChanged Add(Int32 index = , IEnumerable<T> values = )
public static CollectionChanged Replace(Int32 index = , T value = )
public static CollectionChanged Move(Int32 oldIndex = , Int32 newIndex = , T value = )
public IEnumerable<T> get_Source()
public Void set_Source(IEnumerable<T> value = )
public IEnumerable<T> get_Values()
public Void set_Values(IEnumerable<T> value = )
public T get_Value()
public Int32 get_Index()
public Void set_Index(Int32 value = )
public Int32 get_OldIndex()
public Void set_OldIndex(Int32 value = )
public NotifyCollectionChangedAction get_Action()
public Void set_Action(NotifyCollectionChangedAction value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()