Subscribe to INotifyPropertyChanged for nested (child) objects

后端 未结 6 1263
执念已碎
执念已碎 2020-12-04 22:05

I\'m looking for a clean and elegant solution to handle the INotifyPropertyChanged event of nested (child) objects. Example code:

<         


        
相关标签:
6条回答
  • 2020-12-04 22:35

    I have been Searching the Web for one day now and I found another nice solution from Sacha Barber:

    http://www.codeproject.com/Articles/166530/A-Chained-Property-Observer

    He created weak references within a Chained Property Observer. Checkout the Article if you want to see another great way to implement this feature.

    And I also want to mention a nice implementation with the Reactive Extensions @ http://www.rowanbeach.com/rowan-beach-blog/a-system-reactive-property-change-observer/

    This Solution work only for one Level of Observer, not a full Chain of Observers.

    0 讨论(0)
  • 2020-12-04 22:45

    I wrote an easy helper to do this. You just call BubblePropertyChanged(x => x.BestFriend) in your parent view model. n.b. there is an assumption you have a method called NotifyPropertyChagned in your parent, but you can adapt that.

            /// <summary>
        /// Bubbles up property changed events from a child viewmodel that implements {INotifyPropertyChanged} to the parent keeping
        /// the naming hierarchy in place.
        /// This is useful for nested view models. 
        /// </summary>
        /// <param name="property">Child property that is a viewmodel implementing INotifyPropertyChanged.</param>
        /// <returns></returns>
        public IDisposable BubblePropertyChanged(Expression<Func<INotifyPropertyChanged>> property)
        {
            // This step is relatively expensive but only called once during setup.
            MemberExpression body = (MemberExpression)property.Body;
            var prefix = body.Member.Name + ".";
    
            INotifyPropertyChanged child = property.Compile().Invoke();
    
            PropertyChangedEventHandler handler = (sender, e) =>
            {
                this.NotifyPropertyChanged(prefix + e.PropertyName);
            };
    
            child.PropertyChanged += handler;
    
            return Disposable.Create(() => { child.PropertyChanged -= handler; });
        }
    
    0 讨论(0)
  • 2020-12-04 22:45

    Check-out my solution on CodeProject: http://www.codeproject.com/Articles/775831/INotifyPropertyChanged-propagator It does exactly what you need - helps to propagate (in elegant way) dependent properties when relevant dependencies in this or any nested view models change:

    public decimal ExchTotalPrice
    {
        get
        {
            RaiseMeWhen(this, has => has.Changed(_ => _.TotalPrice));
            RaiseMeWhen(ExchangeRate, has => has.Changed(_ => _.Rate));
            return TotalPrice * ExchangeRate.Rate;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 22:50

    since I wasn't able to find a ready-to-use solution, I've done a custom implementation based on Pieters (and Marks) suggestions (thanks!).

    Using the classes, you will be notified about any change in a deep object tree, this works for any INotifyPropertyChanged implementing Types and INotifyCollectionChanged* implementing collections (Obviously, I'm using the ObservableCollection for that).

    I hope this turned out to be a quite clean and elegant solution, it's not fully tested though and there is room for enhancements. It's pretty easy to use, just create an instance of ChangeListener using it's static Create method and passing your INotifyPropertyChanged:

    var listener = ChangeListener.Create(myViewModel);
    listener.PropertyChanged += 
        new PropertyChangedEventHandler(listener_PropertyChanged);
    

    the PropertyChangedEventArgs provide a PropertyName which will be always the full "path" of your Objects. For example, if you change your Persons's "BestFriend" Name, the PropertyName will be "BestFriend.Name", if the BestFriend has a collection of Children and you change it's Age, the value will be "BestFriend.Children[].Age" and so on. Don't forget to Dispose when your object is destroyed, then it will (hopefully) completely unsubscribe from all event listeners.

    It compiles in .NET (Tested in 4) and Silverlight (Tested in 4). Because the code in seperated in three classes, I've posted the code to gist 705450 where you can grab it all: https://gist.github.com/705450 **

    *) One reason that the code is working is that the ObservableCollection also implements INotifyPropertyChanged, else it wouldn't work as desired, this is a known caveat

    **) Use for free, released under MIT License

    0 讨论(0)
  • 2020-12-04 22:51

    I think what you're looking for is something like WPF binding.

    How INotifyPropertyChanged works is that the RaisePropertyChanged("BestFriend"); must only be fored when the property BestFriend changes. Not when anything on the object itself changes.

    How you would implement this is by a two step INotifyPropertyChanged event handler. Your listener would register on the changed event of the Person. When the BestFriend gets set/changed, you register on the changed event of the BestFriend Person. Then, you start listening on changed events of that object.

    This is exactly how WPF binding implements this. The listening to changes of nested objects is done through that system.

    The reason this is not going to work when you implement it in Person is that the levels can become very deep and the changed event of BestFriend does not mean anything anymore ("what has changed?"). This problem gets larger when you have circular relations where e.g. the best friend of your monther is the mother of your best fiend. Then, when one of the properties change, you get a stack overflow.

    So, how you would solve this is to create a class with which you can build listeners. You would for example build a listener on BestFriend.FirstName. That class would then put an event handler on the changed event of Person and listen to changes on BestFriend. Then, when that changes, it puts a listener on BestFriend and listens for changes of FirstName. Then, when that changes, it sends raises an event and you can then listen to that. That's basically how WPF binding works.

    See http://msdn.microsoft.com/en-us/library/ms750413.aspx for more information on WPF binding.

    0 讨论(0)
  • 2020-12-04 22:53

    Interesting solution Thomas.

    I found another solution. It's called Propagator design pattern. You can find more on the web (e.g. on CodeProject: Propagator in C# - An Alternative to the Observer Design Pattern).

    Basically, it's a pattern for updating objects in a dependency network. It is very useful when state changes need to be pushed through a network of objects. A state change is represented by an object itself which travels through the network of Propagators. By encapsulating the state change as an object, the Propagators become loosely coupled.

    A class diagram of the re-usable Propagator classes:

    A class diagram of the re-usable Propagator classes

    Read more on CodeProject.

    0 讨论(0)
提交回复
热议问题