views:

29

answers:

1

In a poco view model I might have a calculated property that pulls data from several different properties inside its get. When these properties values change I need to remember to call OnNotifyPropertyChanged for the calculated property. I was thinking that any line of code where I say NotifyPropertyChanged is not testable code, so I am experiementing with a dependency object approach. I find myself in a different situation where I have to set the calculated dependency property in callbacks for any of the properties it relies on. The properties used in the calculation are now pushing the value to the calculated dependency property. Although this seems a bit more testable it is also feels like more work. I would prefer to pull this information somehow and not have to call OnNotifyPropertyChanged. Is this possible?

Below is my code. Caption2 depends on Caption. This code works fine I would just prefer to not have to use a callback.

     public class TestDataContext : DependencyObject
    {

        private static void OnCaptionPropertyChanged(DependencyObject dependencyObject,
               DependencyPropertyChangedEventArgs e)
        {
            TestDataContext myUserControl = dependencyObject as TestDataContext;
            myUserControl.Caption2 = "Hello " + e.NewValue;

        }


        public static readonly DependencyProperty CaptionProperty =
  DependencyProperty.Register(" World", typeof(string), typeof(TestDataContext),
  new PropertyMetadata("DefaultValue", OnCaptionPropertyChanged));


        public static readonly DependencyProperty Caption2Property =
DependencyProperty.Register("Caption2", typeof(string), typeof(TestDataContext),
new PropertyMetadata("Hello" + TestDataContext.CaptionProperty));

        public string Caption
        {
            get { return GetValue(CaptionProperty).ToString(); }
            set { SetValue(CaptionProperty, value); }
        }

        public string Caption2
        {
            get { return GetValue(Caption2Property).ToString(); }
            set { SetValue(Caption2Property, value); }
        }


    }
+1  A: 

"I was thinking that any line of code where I say NotifyPropertyChanged". Why is that?

Aliostad
If you have a calculated field in a poco view model that pulls values you would be required to notify the bindings in wpf by calling NotifyPropertyChanged. The bindings are not included in the unit tests and therefore you can leave this out and all your test should still pass. Lets think of an example where you leave it out. In your unit test you change a property that is referenced by the calculated property. Your test will pass because it pulls the calculated value in the unit test. However the wpf application does not pull anything because it was never told. Am I missing something?
Chris Perry