Search code examples
c#delegatesinotifypropertychangedinotifycollectionchanged

C#: Subscribing to event fired from another event


I'm trying to raise PropertyChangedEventHandler from a CollectionChanged callback. It gets raised, but it doesn't reach the subscriber.

Perhaps I can't just treat an EventHandler like a variable? Although I have done so without issue in the past, it's just raising it from within another event where I am having issue.

Any help appreciated, thanks.

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Fire
{
    /// <summary>
    /// Simple class implementing INotifyPropertyChanged
    /// I want to raise PropertyChanged whenever the ObservableCollection changes.
    /// </summary>
    public class Model : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public ObservableCollection<int> Collection { get; set; }

        public Model()
        {
            Collection = new ObservableCollection<int>();

            // In theory it should be sorted out here:
            Utils.RaisePropertyChangedWhenCollectionChanged(this, Collection, PropertyChanged, "Collection");
        }
    }

    /// <summary>
    /// I want to wrap the "If CollectionChanged then raise PropertyChanged" logic in a utility method".
    /// </summary>
    public class Utils
    {
        public static void RaisePropertyChangedWhenCollectionChanged(object owner, INotifyCollectionChanged collection, PropertyChangedEventHandler eventHandler, string propertyName)
        {
            collection.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs e) =>
            {
                eventHandler(owner, new PropertyChangedEventArgs(propertyName));
            };
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Model model = new Model();

            model.PropertyChanged += model_PropertyChanged;

            // But the problem is that PropertyChanged does not fire, even when the collection is changed.
            model.Collection.Add(2);
        }

        static void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // This does not get called!
            Console.Out.WriteLine(String.Format("{0}", e.PropertyName));
        }
    }
}

Solution

  • In this call:

    Utils.RaisePropertyChangedWhenCollectionChanged(this, Collection, PropertyChanged, "Collection");
    

    ... you're passing in the current value of PropertyChanged, which is a single no-op delegate. Instead, you want it to raise the property changed event for whatever handlers are present when the event is raised. For example:

    Utils.RaisePropertyChangedWhenCollectionChanged
         (this, Collection, 
          (sender, args) => PropertyChanged(sender, args),
          "Collection");