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.