x:绑定到DependencyProperty不起作用(经典绑定工作正常)

Ser*_*694 4 c# wpf xaml windows-runtime uwp

我在将经典绑定移植到UWP应用程序中的新编译绑定时遇到了一些问题.

我有一个简单的UserControl DependencyProperty:

public double Value
{
    get { return (double)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(nameof(Value), typeof(double), typeof(MyUserControl),
    new PropertyMetadata(0d, OnValuePropertyChanged));

private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // Get the new value and do stuff on the control
}
Run Code Online (Sandbox Code Playgroud)

在我的页面代码隐藏文件中,我分配了DataContext并为编译的绑定创建了一个参数:

public MyPage()
{
    this.InitializeComponent();
    DataContext = new MyPageViewModel();
}

public MyPageViewModel ViewModel => (MyPageViewModel)DataContext;
Run Code Online (Sandbox Code Playgroud)

现在,这个经典绑定工作(目标参数正确实现了INotifyPropertyChanged接口):

<controls:MyUserControl Value="{Binding MyValue}"/>
Run Code Online (Sandbox Code Playgroud)

但是这个编译的绑定不会:

<controls:MyUserControl Value="{x:Bind ViewModel.MyValue}"/>
Run Code Online (Sandbox Code Playgroud)

编译器没有给我一个错误,因此它在构建应用程序时确实找到了target属性,但在运行时它只是不起作用.

我想我错过了一些非常明显和愚蠢的东西,但我只是不知道究竟是什么.预先感谢您的帮助!

Igo*_*lic 7

"经典"绑定和最新编译绑定(x:Bind)之间最烦人的区别是默认绑定模式.对于经典Binding,默认值是OneWay但对于x:绑定默认值为OneTime,因此当您更改属性值时,它不会反映在UI中,因为绑定只会获取一次值而不关心任何将来的更改通知.

Value="{x:Bind ViewModel.MyValue, Mode=OneWay}"
Run Code Online (Sandbox Code Playgroud)