Replacing an observableCollction doesnt raise the PropertyChange event RRS feed

  • Question

  • I saw a description of my problem in an article on the internet, I have the same problem - but no solution was suggested.

    If someone could tell me what to do in order to get over it, I'll be grateful.

    Replacing the collection

    When a view binds to an ObservableCollection, it subscribes to events from that instance. It expects to be notified when items are added, removed, or replaced within that instance of the collection. The binding is not to the property of the parent object, but to that specific instance of the collection itself.

    A common mistake is to modify the parent object’s property and replace one ObservableCollection with another. Just about every person who uses ObservableCollections makes this mistake at least once. Even Scott Hanselman made this mistake on stage at Mix ‘09 (skip to 10:30). The problem is that after you swap out one ObservableCollection for a new one, the view is still bound to the old one.

    A common “fix” for this problem is to fire a PropertyChanged event to notify the view that the collection has been replaced. This causes the view to unbind from the old one and rebind to the new one. In this situation, the view can’t adjust to just the items that were added or removed; it has to rebind the entire list box. Not only is this inefficient, but it also scrolls back to the top of the list rather than preserving the user’s context.

    This fix completely obviates the need for an ObservableCollection in the first place. It wasn’t the collection that changed, it was the property that points to the collection. That property could have pointed to a List, and it would have had the same effect. Which brings us to the next common mistake.

    What can I do other that fire the prepertyChanged event by myself (when I replace my observable collection)?

    Sunday, November 16, 2014 8:24 PM


  • >>What can I do other that fire the prepertyChanged event by myself (when I replace my observable collection)?

    "Other than fore the PropertyChanged event ..."? Nothing. If you replace the collection, you must raise the PropertyChanged event for the collection property to which the ItemsSource property of some ItemsControl is bound to. For example if you bind to a "YourCollection" property and replaces this collection with another one:

    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<String> _coll:
            public ObservableCollection<String> YourCollection
                    return _coll;
                    _coll = value;
                        if (PropertyChanged != null)
                            PropertyChanged(this, new PropertyChangedEventArgs("YourCollection"));
    this.YourCollection = new ObservableCollection<string>();

    But instead of setting the property to a new collection you could call the Clear() method on it and then add the new items to it:

    //add new items...
    this.YourCollection.Add("new item...");

    Then you don't need to raise the PropertyCanged event in the setter of the collection property.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by sara_ss Monday, November 17, 2014 7:25 AM
    Sunday, November 16, 2014 9:14 PM