0

これは、ここでの質問の続きです:カスタム DependencyObject をセットアップしようとしています。明らかに何かが欠けています。元の質問を編集するのは現実的ではありません。変化が大きすぎる。だから私は新鮮な質問を始めています。

UWP アプリでカスタム DependencyObjects 間のバインドをセットアップしようとしています。関連するコードは以下です。ActualWidthPropertyChanged への呼び出しが表示されていますが、WidthPropertyChanged への呼び出しをトリガーしていません。私は何が欠けていますか?

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

2 に答える 2

1

ActualWidthPropertyChanged への呼び出しが表示されていますが、WidthPropertyChanged への呼び出しをトリガーしていません。私は何が欠けていますか?

この問題を解決するには、ソース オブジェクトに INotifyPropertyChanged インターフェイスを実装して、ソースが変更を報告できるようにする必要があります。

次のコードを参照してください。

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);
    }
}
于 2016-12-06T08:25:23.193 に答える