Imagine that you’d like to be notified when something is changed in a collection, e.g. an item is added or removed. One possible solution is to use the built-in .NET generic collection type ObservableCollection of T which is located in the System.Collections.ObjectModel namespace. The ObservableCollection object has an event called CollectionChanged. You can hook up an event handler to be notified of the changes.
If you don’t know what events, event handlers and delegates mean then start
here
.
Let’s see a simple example with a collection of strings:
ObservableCollection<string> names = new ObservableCollection<string>();
names.Add("Adam");
names.Add("Eve");
names.Add("Clive");
names.Add("Anne");
names.Add("John");
names.Add("Peter");
names.Add("Hannah");
Nothing special so far I guess. This looks exactly like a “normal” collection.
The following example demonstrates the CollectionChanged event:
private static void RunObservableCollectionCode()
ObservableCollection<string> names = new ObservableCollection<string>();
names.CollectionChanged += names_CollectionChanged;
names.Add("Adam");
names.Add("Eve");
names.Remove("Adam");
names.Add("John");
names.Add("Peter");
names.Clear();
static void names_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
Debug.WriteLine("Change type: " + e.Action);
if (e.NewItems != null)
Debug.WriteLine("Items added: ");
foreach (var item in e.NewItems)
Debug.WriteLine(item);
if (e.OldItems != null)
Debug.WriteLine("Items removed: ");
foreach (var item in e.OldItems)
Debug.WriteLine(item);
The NotifyCollectionChangedEventArgs object resides in the System.Collections.Specialized namespace.
If I run the RunObservableCollectionCode method I get the following output:
Change type: Add
Items added:
Change type: Add
Items added:
Change type: Remove
Items removed:
Change type: Add
Items added:
Change type: Add
Items added:
Peter
Change type: Reset
You can then decide how to handle additions and deletions in the event handler.
View all various C# language feature related posts
here
.
Like this:
Like
Loading...
Related
Enter your email address to follow this blog and receive notifications of new posts by email.
Email Address:
History
History
Select Month
October 2020 (12)
April 2018 (1)
February 2018 (4)
January 2018 (7)
December 2017 (3)
November 2017 (14)
October 2017 (30)
September 2017 (30)
August 2017 (31)
July 2017 (31)
June 2017 (30)
May 2017 (31)
April 2017 (30)
March 2017 (31)
February 2017 (28)
January 2017 (31)
December 2016 (31)
November 2016 (30)
October 2016 (31)
September 2016 (30)
August 2016 (22)
July 2016 (5)
June 2016 (8)
May 2016 (26)
April 2016 (17)
March 2016 (25)
February 2016 (27)
January 2016 (20)
December 2015 (24)
November 2015 (30)
October 2015 (30)
September 2015 (30)
August 2015 (30)
July 2015 (31)
June 2015 (30)
May 2015 (31)
April 2015 (30)
March 2015 (31)
February 2015 (28)
January 2015 (31)
December 2014 (31)
November 2014 (30)
October 2014 (23)
September 2014 (22)
August 2014 (21)
July 2014 (25)
June 2014 (21)
May 2014 (22)
April 2014 (22)
March 2014 (20)
February 2014 (19)
January 2014 (21)
December 2013 (9)
November 2013 (8)
October 2013 (10)
September 2013 (9)
August 2013 (9)
July 2013 (8)
June 2013 (8)
May 2013 (6)
April 2013 (8)
March 2013 (8)
February 2013 (8)
January 2013 (10)
December 2012 (1)
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy