0

UWPアプリには次のものがあります。私の Element は、Control や Panel などからではなく、DependencyObject から派生していることに注意してください。しかし、テスト メソッド MessWithBindings() を呼び出すと、バインディングが機能しません。PropertyChanged メソッドは起動しますが、PropertyChangedEventHandler は null であるため、何もしません。どうにかしてイベントハンドラーを直接セットアップすることになっていますか? または、それを作成する行方不明の別の呼び出しがありますか?

ちなみに、BindWidth/BindHeight メソッドの FrameworkElement バージョンがあり、それらは問題なく動作します。

class WindowsElement: DependencyObject, INotifyPropertyChanged
{
    public WindowsElement()
    {
    }
    private double _Width { get; set; }
    private double _Height { get; set; }
    public double Width
    {
        get
        {
            return _Width;
        }
        set
        {
            if (_Width != value)
            {
                _Width = value;
                OnPropertyChanged("Width");
            }
        }
    }


    public double Height
    {
        get
        {
            return _Height;
        }
        set
        {
            if (_Height != value)
            {
                _Height = value;
                OnPropertyChanged("Height");
            }
        }
    }

    public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
        "Width",
        typeof(double),
        typeof(WindowsElement),
        null);

    public static readonly DependencyProperty HeightProperty = DependencyProperty.Register(
"Height",
typeof(double),
typeof(WindowsElement),
null);

    private double _ActualWidth { get; set; }

    public double ActualWidth {
        get
        {
            return _ActualWidth;
        }
        set
        {
            if (_ActualWidth != value)
            {
                _ActualWidth = value;
                OnPropertyChanged("ActualWidth");
            }
        }
    }

    private double _ActualHeight { get; set; }

    public double ActualHeight {
        get
        {
            return _ActualHeight;
        }
        set
        {
            if (_ActualHeight != value)
            {
                _ActualHeight = value;
                OnPropertyChanged("ActualHeight");
            }
        }
    }

    public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(
        "ActualWidth",
        typeof(double),
        typeof(WindowsElement),
        null);

    public static readonly DependencyProperty ActualHeightProperty = DependencyProperty.Register(
"ActualHeight",
typeof(double),
typeof(WindowsElement),
null);

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    internal void SetBinding(DependencyProperty property, Binding b)
    {
        BindingOperations.SetBinding(this, property, b);
    }

    public static void MessWithBindings()
    {
        WindowsElement we1 = new WindowsElement();
        WindowsElement we2 = new WindowsElement();
        we1.BindWidth(we2);
        we1.BindHeight(we2);
        we2.Width = 12;
        we2.ActualWidth = 13;
        we2.ActualHeight = 666;
        CommonDebug.LogLine(we1, we1.Width, we1.Height, we1.ActualWidth, we1.ActualHeight, we2, we2.ActualWidth, we2.ActualHeight);
        CommonDebug.DoNothing();
    }
}


internal static class WindowsElementAdditions
{
  public static void BindWidth(this WindowsElement bindMe, WindowsElement toMe)
  {
    Binding b = new Binding();
    b.Mode = BindingMode.OneWay;
    b.Source = toMe.ActualWidth;
    bindMe.SetBinding(WindowsElement.WidthProperty, b);
  }

  public static void BindHeight(this WindowsElement bindMe, WindowsElement toMe)
  {
    Binding b = new Binding();
    b.Mode = BindingMode.OneWay;
    b.Source = toMe.ActualHeight;
    bindMe.SetBinding(WindowsElement.HeightProperty, b);
  }
}
4

1 に答える 1