C# Displaying String of ObservableCollection on event CollectionChanged

617 views Asked by At

I'm just learning events in C# and was given the following example in a tutorial. My question is how I can display the contents of the string added/removed in my event handler.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SubscribeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var coll = new ObservableCollection<string>();

            // TODO: subscribe to an event here
            coll.CollectionChanged += coll_CollectionChanged;

            coll.Add("Big Mac");
            coll.Add("Filet 'O Fish");
            coll.Add("Quarter Pounder");
            coll.Add("French Fries");
            coll.Remove("Filet 'O Fish");

            Console.ReadKey(true);
        }

        static void coll_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //Does not do what I want it to.
            Console.WriteLine(Convert.ToString(e.NewItems));
        }
    }
}
1

There are 1 answers

2
Nico On BEST ANSWER

The Observable Collection, The Collection Changed and The Event Args documentation are pretty straight forward.

In the changed event you have the properties NewItems and OldItems (among others) that contain the newly added or removed objects from the collection. Now these are a simple IList (not to be confused with IList<T>) so you have to do some casting. As we know the collection is a string we would expect the NewItems or the OldItems collection to contain string values.

Now these properties are null if they are not applicable. ie. In a Add method (or action) the OldItems property will be null. Therefore if you just want to print the changes try below.

static void coll_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    List<string> items = new List<string>();

    //added items
    if (e.NewItems != null)
        items.AddRange(e.NewItems.OfType<string>());

    ///old items
    if (e.OldItems != null)
        items.AddRange(e.OldItems.OfType<string>());

    Console.WriteLine(string.Join(", ", items));
}

Full Changed Code: Again this doesnt worry about the action just prints the results.

class Program
{
    static void Main(string[] args)
    {
        var coll = new ObservableCollection<string>();

        // TODO: subscribe to an event here
        coll.CollectionChanged += coll_CollectionChanged;

        coll.Add("Big Mac");
        coll.Add("Filet 'O Fish");
        coll.Add("Quarter Pounder");
        coll.Add("French Fries");
        coll.Remove("Filet 'O Fish");

        Console.ReadKey(true);
    }

    static void coll_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        List<string> items = new List<string>();

        //added items
        if (e.NewItems != null)
            items.AddRange(e.NewItems.OfType<string>());

        ///old items
        if (e.OldItems != null)
            items.AddRange(e.OldItems.OfType<string>());

        Console.WriteLine(string.Join(", ", items));
    }
}