【发布时间】:2016-01-10 02:38:52
【问题描述】:
我在将经典绑定移植到 UWP 应用中新编译的绑定时遇到了一些问题。
我有一个带有简单 DependencyProperty 的 UserControl:
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
}
在我的页面代码隐藏文件中,我分配 DataContext 并为编译的绑定创建一个参数:
public MyPage()
{
this.InitializeComponent();
DataContext = new MyPageViewModel();
}
public MyPageViewModel ViewModel => (MyPageViewModel)DataContext;
现在,这个经典绑定工作(目标参数正确实现了 INotifyPropertyChanged 接口):
<controls:MyUserControl Value="{Binding MyValue}"/>
但是这个编译的绑定没有:
<controls:MyUserControl Value="{x:Bind ViewModel.MyValue}"/>
编译器没有给我一个错误,所以它在构建应用程序时确实找到了目标属性,但在运行时它就不起作用了。
我想我在这里遗漏了一些非常明显和愚蠢的东西,但我只是不知道它到底是什么。提前感谢您的帮助!
【问题讨论】:
标签: c# wpf xaml windows-runtime uwp