简体   繁体   中英

Subscribe to base class event from derived class

I have been struggling to subscribe to a base class event from a derived class. The purpose is to raise the event in my base class every time i get a propertychanged and consequently run a method in my derived class. Here is a reduced version of my code:

namespace MyNamespace
{
public delegate void EventHandler();
[Serializable]
public class ChartGroupCollection : ObservableCollection<ChartGroup>, INotifyCollectionChanged
{

    public ChartGroupCollection()
    {
       base.DirtyFlagging += new EventHandler(MethodIWantToRun); //subscription to event
    }

    void MethodIWantToRun()
    {
        SomeVariable++;
    }


    #region NotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}


[Serializable]
public class ChartGroup : INotifyPropertyChanged
{
    public event EventHandler<EventArgs> DirtyFlagging;
    protected virtual void OnDirtyFlagging(EventArgs e)
    {
        EventHandler<EventArgs> handler = DirtyFlagging;
        if (handler != null)
        {
            handler(this, e);
        }
    }


    public ChartGroup()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            DirtyFlagging.Invoke(this, someeventargs);  //Where i want to invoke the event
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

I think you want to do something like this

public class ChartGroupCollection : ObservableCollection<ChartGroup>
{
    public int SomeVariable { get; set; }

    public ChartGroupCollection()
    {
        CollectionChanged += ChartGroupCollection_CollectionChanged;
    }

    void ChartGroupCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.OldItems != null)
        {
            foreach (ChartGroup item in e.OldItems)
            {
                item.PropertyChanged -= item_PropertyChanged;
            }
        }

        if (e.NewItems != null)
        {
            foreach (ChartGroup item in e.NewItems)
            {
                item.PropertyChanged += item_PropertyChanged;
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MethodIWantToRun(); 
    }

    private void MethodIWantToRun()
    {
        SomeVariable++;
    }
}

public class ChartGroup : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    int _myProperty;
    public int MyProperty
    {
        set {
            _myProperty = value;
            NotifyPropertyChanged();
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Call to trigger event:

ChartGroupCollection chartGroupCollection = new ChartGroupCollection();
var group = new ChartGroup();
chartGroupCollection.Add(group);
group.MyProperty = 3;

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