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); } } }