0

Hopefully someone can help me out with this. I am creating a Silverlight app which is used for editing images. A user has a project which contain layers which contain elements. (Elements are text and image elements).

I have a class which represents the project. It contains an ObservableCollection<Layer> and each Layer has an ObservableCollection<Element>. Element is an abstract class. There is a TextElement and ImageElement class which inherit from Element.

My problem is the UI never gets updated when I change an element inside the collection. I am using INotifyPropertyChanged on all my properties and I am catching CollectionChanged on the collections but still no go. The CollectionChanged event for ObservableCollection<Element> never gets hit on an update of one of its elements.

This is the code I had originally had:

void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { this.NotifyChange("Elements"); }

If anyone can help I would be very grateful.

Matthew
  • 3
  • 5

1 Answers1

0

ObservableCollection<> publishes CollectionChanged when items are added or removed, but it does NOT do so when an item inside the collection changes.

In order to get around this, you could subscribe to the CollectionChanged event and then subscribe to the INotifyPropertyChanged of added elements inside the CollectionChanged handler:

elementCollection.CollectionChanged += (s, a) =>
{
    foreach (Element element in a.NewItems)
        element.PropertyChanged += ElementChanged;
};

Then you can update the UI within the ElementChanged method.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Thanks for your advice but the problem is that the elementCollection.CollectionChanged event never fires. The properties of the elements get updated but the Collection containing the Elements does not. – Matthew Jun 11 '12 at 19:31
  • CollectionChanged won't get fired when properties of the elements change but it _will_ fire when elements are added to the collection. So using the code above, you'll have already subscribed to the PropertyChanged of the element when it was added to the collection -- and later when the property of that element changes, CollectionChanged won't fire but PropertyChanged of the element will fire and ElementChanged method will run. – Eren Ersönmez Jun 11 '12 at 19:41
  • I am not sure I understand. I have implemented the code above but it only runs when I add an element. Is there any way I can have the UI updated as I change a property eg. Element.Name? – Matthew Jun 11 '12 at 19:50
  • What shall I put into ElementChanged? – Matthew Jun 12 '12 at 09:54
  • you could try putting `this.NotifyChange("Elements")` in the `ElementChanged` method. – Eren Ersönmez Jun 12 '12 at 10:00
  • Ya, that is what I was doing but it is not working for me. I have created another post about this with code here: http://stackoverflow.com/questions/10992832/update-observablecollecttion-on-objects-property-changed – Matthew Jun 12 '12 at 10:04
  • Well, that's NOT what you were doing. You had `this.NotifyChange("Elements");` inside the handler for CollectionChanged. As I said before, CollectionChanged won't fire when an element changes. The code I recommended above _should_ have ElementChanged fire when an element changes. So if you put `this.NotifyChange("Elements");` in `ElementChanged` it should accomplish what you intended to do in the CollectionChanged handler. Did you actually this code I recommended? – Eren Ersönmez Jun 12 '12 at 10:13
  • Could you please show me using my code in the other post what you mean? I thought I was implementing you code if you look at my Layer class on the other post. Thanks for your patience. – Matthew Jun 12 '12 at 10:24