简体   繁体   中英

C# Displaying String of ObservableCollection on event CollectionChanged

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));
        }
    }
}

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));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM