Set bindings for custom DependencyObjects

余生颓废 提交于 2019-12-25 08:23:31

问题


This is a continuation of a question here: Trying to setup a custom DependencyObject. Clearly missing something. It's not practical to edit the original question; changes are too great. So I'm starting a fresh question.

I'm trying to setup bindings between custom DependencyObjects in my UWP app. The relevant code is below. I am seeing calls to ActualWidthPropertyChanged, but they are not triggering any call to WidthPropertyChanged. What am I missing?

class WindowsElement: DependencyObject
{
    public WindowsElement()
    {
    }
    public double Width
    {
        get
        {
          return (double)GetValue(WidthProperty);
        }
        set
        {
          SetValue(WidthProperty, value);
        }
    }

    private static void WidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
      WindowsElement element = (WindowsElement)o;
      double width = (double)e.NewValue;
      CommonDebug.LogLine("WPC", element, o, width);
      element.Width = width;
    }

   private static void ActualWidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
     {
       WindowsElement element = (WindowsElement)o;
       double width = (double)e.NewValue;
       CommonDebug.LogLine("AWPC", o, e, width, element.Width);
       element.ActualWidth = width;
     }
     public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
        "Width",
        typeof(double),
        typeof(WindowsElement),
       new PropertyMetadata((double)0, WidthPropertyChanged));

      public double ActualWidth {
        get
          {
            return (double)GetValue(ActualWidthProperty);
          }
        set
            {
              SetValue(ActualWidthProperty, value);
            }
        }


    public static readonly DependencyProperty ActualWidthProperty =  
      DependencyProperty.Register(
        "ActualWidth",
        typeof(double),
        typeof(WindowsElement),
        new PropertyMetadata((double)0, ActualWidthPropertyChanged));


    public static void MessWithBindings()
    {
        WindowsElement we1 = new WindowsElement();
        WindowsElement we2 = new WindowsElement();
        var b = new Binding
          {
            Source = we2,
            Path = new PropertyPath("ActualWidth")
          };

        BindingOperations.SetBinding(we1, WindowsElement.WidthProperty, b);
        we2.ActualWidth = 13;
        CommonDebug.LogLine(we1, we1.Width,  we1.ActualWidth, we2, we2.Width, we2.ActualWidth);
    }
}

回答1:


I am seeing calls to ActualWidthPropertyChanged, but they are not triggering any call to WidthPropertyChanged. What am I missing?

To solve this question, you would need to implement the INotifyPropertyChanged interface on the source object so that the source can report changes.

Please see the following code:

class WindowsElement : DependencyObject, INotifyPropertyChanged
{
    public WindowsElement()
    {
    }

    public double Width
    {
        get
        {
            return (double)GetValue(WidthProperty);
        }
        set
        {
            SetValue(WidthProperty, value);
        }
    }

    private static void WidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        WindowsElement element = (WindowsElement)o;
        double width = (double)e.NewValue;
        CommonDebug.LogLine("WPC", element, o, width);
        //element.Width = width;
        element.RaisedPropertyChanged("Width");
    }

    private static void ActualWidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        WindowsElement element = (WindowsElement)o;
        double width = (double)e.NewValue;
        CommonDebug.LogLine("AWPC", o, e, width, element.Width);
        //element.ActualWidth = width;
        element.RaisedPropertyChanged("ActualWidth");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisedPropertyChanged(string PropertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }

    public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
        "Width",
        typeof(double),
        typeof(WindowsElement),
        new PropertyMetadata((double)0, WidthPropertyChanged));

    public double ActualWidth
    {
        get
        {
            return (double)GetValue(ActualWidthProperty);
        }
        set
        {
            SetValue(ActualWidthProperty, value);
        }
    }

    public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(
        "ActualWidth",
        typeof(double),
        typeof(WindowsElement),
        new PropertyMetadata((double)0, ActualWidthPropertyChanged));

    public static void MessWithBindings()
    {
        WindowsElement we1 = new WindowsElement();
        WindowsElement we2 = new WindowsElement();
        var b = new Binding
        {
            Source = we2,
            Path = new PropertyPath("ActualWidth")
        };

        BindingOperations.SetBinding(we1, WindowsElement.WidthProperty, b);
        we2.ActualWidth = 13;
        CommonDebug.LogLine(we1, we1.Width, we1.ActualWidth, we2, we2.Width, we2.ActualWidth);
    }
}



回答2:


Not sure why in UWP a one-way Binding from one dependency property to another doesn't automatically update the target property (as it does in WPF).

However, you could simply revert the direction of the Binding and make it two-way:

var b = new Binding
{
    Source = we1,
    Path = new PropertyPath("Width"),
    Mode = BindingMode.TwoWay
};

BindingOperations.SetBinding(we2, WindowsElement.ActualWidthProperty, b);
we2.ActualWidth = 13;


来源:https://stackoverflow.com/questions/40976591/set-bindings-for-custom-dependencyobjects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!