Dependency Property dependent on another

后端 未结 2 860
忘掉有多难
忘掉有多难 2020-12-25 12:34

How can one register a dependency property whose value is calculated using the value of another dependency property?

Because the .NET property wrappers are bypassed

相关标签:
2条回答
  • 2020-12-25 13:25

    Hmmm... I think you'd better look at dependency properties value coercion. Here is an example with coercion:

    public class ViewModel : DependencyObject
    {
      public bool TestBool
      {
        get { return (bool)GetValue(TestBoolProperty); }
        set { SetValue(TestBoolProperty, value); }
      }
      public static readonly DependencyProperty TestBoolProperty =
        DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel), new PropertyMetadata(false, OnTestBoolPropertyChanged));
    
      private static void OnTestBoolPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
        var vm = (ViewModel)d;
        vm.CoerceValue(TestDoubleProperty);
      }
    
      public double TestDouble
      {
        get { return ((double)GetValue(TestDoubleProperty)); }
        set { SetValue(TestDoubleProperty, value); }
      }
      public static readonly DependencyProperty TestDoubleProperty =
        DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel), new PropertyMetadata(0.0, null, OnCoerceTestDouble));
    
      private static object OnCoerceTestDouble(DependencyObject d, object baseValue)
      {
        var vm = (ViewModel) d;
        var testBool = vm.TestBool;
        return ((testBool) ? (100.0) : (200.0));
      }
    }
    
    0 讨论(0)
  • 2020-12-25 13:32

    You're actually correct, you should use PropertyChangedCallback. Here's how:

    public bool TestBool
    {
      get { return (bool)GetValue(TestBoolProperty); }
      set 
      { 
        SetValue(TestBoolProperty, value);
      }
    }
    public static readonly DependencyProperty TestBoolProperty =
      DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel),
      new PropertyMetadata(false, new PropertyChangedCallback(OnTestBoolChanged)));
    
    private static void OnTestBoolChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      ViewModel vm = d as ViewModel;
      vm.TestDouble = value ? 100.0 : 200.0;
    }
    
    public double TestDouble
    {
      get { return ((double)GetValue(TestDoubleProperty)); }
      set { SetValue(TestDoubleProperty, value); }
    }
    public static readonly DependencyProperty TestDoubleProperty =
      DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel));
    
    0 讨论(0)
提交回复
热议问题