Questions about ChangeNotificationWrapper

May 12, 2014 at 2:06 PM
Geert,
I am trying to get away from using the messenger and am trying to use the ChangeNotificationWrapper with constructor injection to track changes of a property. I have a model based on the ModelBase,
    public class DomainManager : ModelBase, IDomainManager
    {
        public bool IsTimeDomain { get; set; }

        internal void OnIsTimeDomainChanged()
        {
            return;
        }
    }
I have a second model ,
    public class DisplayScalingManager : ModelBase, IDisplayScalingManager
        private void Initialize(IDomainManager domainManager)
        {
            this.wrapper = new ChangeNotificationWrapper(domainManager);
            if (!this.wrapper.SupportsNotifyPropertyChanged)
            {
                return;
            }

            var args = new AdvancedPropertyChangedEventArgs(domainManager, "IsTimeDomain");
            this.wrapper.PropertyChanged += this.OnPropertyChanged(args);
        }
The this.OnPropertyChanged(args) added to PropertyChanged gives a "argument type "void" not assignable to parameter type System.ComponentModel.PropertyChangedEventHanadler" error and will not compile. I am trying to perform an action in the DisplayScalingManager when DomainManager.IsTimeDomain changes.
  1. Can you give me some guidance?
  2. Is this a good approach?
Thanks in advance
Bob
Coordinator
May 13, 2014 at 7:36 AM
wrapper.PropertyChanged += this.OnPropertyChanged(args); will not work. This is because you need a handler, not a method. A quick and dirty solution would be something like this:
wrapper.PropertyChanged += (sender, e) => this.OnPropertyChanged(new AdvancedPropertyChangedEventArgs(domainManager, "IsTimeDomain"));
Another option is to really pass on what has changed:
wrapper.PropertyChanged += OnChangeNotificationWrapperPropertyChanged;

...

private void OnChangeNotificationWrapperPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var finalEventArgs = new AdvancedPropertyChangedEventArgs(domainManager, e.PropertyName);
    OnPropertyChanged(args);
}
This is a good approach. In the ModelBase of Catel, we use this approach to check for IsDirty. Note that this will always be slower than your superfast POCO objects (which have no events and no subscriptions), but that would only be noticeable if you are instantiating thousands of objects in a short time.
Marked as answer by paco1863 on 5/13/2014 at 6:53 AM
May 13, 2014 at 1:54 PM
Thanks Geert,
This is just what I needed.
Bob