0

how to notify a ObservableCollection<Book> when a Nested Element changes and i need to know it ?

Perhaps a "ObservableCollection<Book>" and "Book" has a Member "Chapter". And "Chapter" has a String Property called "Name" and i change the name value. How to throw CollectionChanged of ObservableCollection when Property "Name" in Name->Chapter -> Book -> ObservableCollection<Book> changed ??

Is the common way in MVVM to tunnel it through manuell so that CollectionChanged is fired when PropertyChange is fired ??

Thanks :)


UPDATE: I found a solution in this post: ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

-> Use Binding List or kind of a TrulyObservableCollection from the post bescause ObservableCollection doesn't relay on item notifications like i already mentioned! difference between ObservableCollection and BindingList

-> If u use BindingList remember the disadvantage of this post http://www.themissingdocs.net/wordpress/?p=465


Fabian
  • 63
  • 11

1 Answers1

0

I've had a similar issue in the past. It was caused by my use of Nested Properties and my lack of Notification Propagation.

I solved it by subscribing to the PropertyChanged event each time my object changed. This way I could update my collection when a Nested Property updated.

Make sure that each simple type (e.g. your Name property) implements OnPropertyChanged, and then each complex type (e.g. your Book and Chapter properties) implements the Notification Propagation.

Your Observable Collection should update accordingly.

Example:

private Book _book;

public Book Book
    {
        get
        {
            return _book;
        }
        set
        {
            if (value == _book|| value == null)
                return;

            if (_book!= null)
                _book.PropertyChanged -= BookPropertyChanged;

            _book= value;
            _book.PropertyChanged += BookPropertyChanged;

            UpdateProperties();
        }
    }

Update Methods:

private void BookPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        OnPropertyChanged(string.Empty);
    }

public void UpdateProperties()
    {
        OnPropertyChanged(string.Empty);
    }

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
Jack
  • 886
  • 7
  • 27
  • Hi, thanks for your answer. My Problem is not that the property changed is not thrown. My Problem is that I want to fire the collectionchanged event of the collection when the propertychanged of a nested object in the collection is thrown. Or maybe I just do not understand something in principle :D – Fabian Mar 27 '18 at 17:33
  • I understand, but your `ObservableCollection` will update if one of its members' properties update. For example, if your `Book` item had a Name property of type `string` and you changed it, then your `ObservableCollection` would update. The issue you are facing is because changing a property of a complex type within your `Book` item will not call `OnPropertyChanged` for your `Book` item. This solution solves that issue and therefore the problem you are facing. – Jack Mar 27 '18 at 17:36
  • In my experience, where I had an `ObservableCollection` of type `Item1`, which contained a property of `Item2`, which contained a property of `Item3`, which then contained simple types that I would be modifying. This solution worked for me. I was able to modify any property in my hierarchy and the `ObservableCollection` would update. – Jack Mar 27 '18 at 17:38
  • You need Property Changed events of nested properties to notify your Collection so that it will update. This will allow you to tunnel the notifications. – Jack Mar 27 '18 at 17:41
  • My User Interface updates if i change the name property in the Collection. I think because WPF uses reflection to look for the INotifyPropertyChanged. But the Collection itself does not get notice of the INotifyPropertyChanged. But i will check it again :/ – Fabian Mar 27 '18 at 17:42