0

In my silver light application i have two classes: Container and Field.

Container contains a list of Fields. Now, whenever an Field(in the list) receives a message I want to notify Container object to perform some logic.

Field object should not contain any reference of Container. i need to achieve this using INotifyPropertyChanged.

Please suggest a simple solution using INotifyPropertyChanged.

1 Answers1

2

Your Container object could subscribe to the PropertyChanged event of each of it's child Field objects.

If it's not a property change your interested in directly you could implement your own custom event for the Field object.

It's hard to given an example without knowing your exact situation... but here goes.

E.g Pseudo-code: (on the Container object)

public void AddField(Field field)
{
    // attach to the new fields property changed event
    field.PropertyChanged += OnFieldPropertyChanged;

    // add the new field to this container's collection
    this.Fields.Add(field);
}

private void OnFieldPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    // do something
}

You may also want to check out ObservableCollections.

Andy
  • 1,060
  • 7
  • 13
  • Added code example of subscribing to the child object's property changed event when it is added to the container. Note: if you are looking at the property changed event specifically, you should look at observable collection and this question: http://stackoverflow.com/questions/269073/observablecollection-that-also-monitors-changes-on-the-elements-in-collection – Andy Feb 11 '12 at 09:17