How to call a public event from a static function in the same class?

I have a class containing an ObservableCollection of another class. I want to be notified if one of the members of the class is changed, because I need to do some calculations in the MediaCollection class. So I added an event to this class:

public event PropertyChangedEventHandler PropertyChangedEvent; 

which is called in this collection class:

 public class MediaCollection : INotifyPropertyChanged { private List<MediaEntry> ModifiedItems = new List<MediaEntry>(); private ObservableCollection<MediaEntry> tagList = new ObservableCollection<MediaEntry>(); public MediaCollection() { tagList = new ObservableCollection<MediaEntry>(); tagList.CollectionChanged += CollectionChangedHandler; } public void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e) { foreach (MediaEntry newItem in e.NewItems) { ModifiedItems.Add(newItem); newItem.PropertyChangedEvent += OnItemPropertyChanged; } ... } public void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e) { MediaEntry item = sender as MediaEntry; if (item != null) ModifiedItems.Add(item); } 

The MediaEntry class looks something like this:

 public class MediaEntry : DependencyObject { public event PropertyChangedEventHandler PropertyChangedEvent; public bool IsError { get { return (bool)GetValue(IsErrorProperty); } set { SetValue(IsErrorProperty, value); } } public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(MediaEntry), new UIPropertyMetadata(PropertyChanged)); public static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { if (obj is MediaEntry) { ((MediaEntry)obj).ObjectPropertyChanged(args); } } 

This call will notify the user interface, etc., but in order to raise the event to the container class, I need to raise my PropertyChangedEvent (which is listened in the container class). According to the documentation, I need to add the following lines:

 public static void PropertyEventChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { PropertyChangedEventHandler handler = PropertyChangedEvent; if (handler != null) { handler(obj, new PropertyChangedEventArgs(args.Property.Name)); } } 

What do I need to call from the public function static void PropertyChanged. However, and here is the real question, how can I trigger a public event from my static function?

I tried many, many things like:

  • Changing a PropertyChangedEventHandler public event to a public static event. This will result in an error like this: "Member MediaEntry.PropertyChangedEvent cannot be accessed using the instance reference, give it a type name instead"

  • Changing the public static void PropertyChanged for the non-static version, but it will give errors for all parts of the UIPropertyMetadata (PropertyChanged) with this error message: "Object reference required for non-static field, method or property"

  • And a few more, but all to no avail.

I somehow understand that I need delegates here, but not now, how and where to start. Any help is greatly appreciated in solving my problem here.

+4
source share
1 answer

When you register IsError DependencyProperty by passing UIPropertyMetadata to it, you set a method that will be called when the property changes. In your case, this is

 public static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 

The instance for which the property has been changed is passed as obj to this method, and in args you find the changed property and the old and new value. This is the place where you should name your event. You just miss the implementation of ObjectPropertyChanged, which is not static, because you are using the argument passed to your PropertyChanged method passed to MediaEntry. The implementation is similar to what you tried to use with the PropertyEventChanged, with the only difference being that it is not static and that you are not passing any object to it:

 public void ObjectPropertyChanged(DependencyPropertyChangedEventArgs args) { PropertyChangedEventHandler handler = PropertyChangedEvent; if (handler != null) { handler(this, new PropertyChangedEventArgs(args.Property.Name)); } } 

BTW I would try to use the best names, since it is very easy to get confused when you read PropertyEventChanged and PropertyChangedEvent and many different combinations of Property and Changed :-).

+2
source

All Articles