我在将我的经典绑定移植到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
}
在我的页面代码隐藏文件中,我分配了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}"/>
编译器不会给我一个错误,所以它在构建应用程序时确实找到了目标属性,但是在运行时它就是不能工作。
我想我错过了一些很明显很愚蠢的东西,但我只是不知道它到底是什么。提前感谢您的帮助!
发布于 2015-10-12 07:35:17
“经典”绑定与最新编译的绑定(x:Bind)之间最恼人的区别是默认绑定模式。对于典型的绑定,缺省值是OneWay
,但是对于x:绑定默认值是OneTime
,所以当您更改属性值时,它不会在UI中反映,因为绑定只获取一次值,而不关心任何未来的更改通知。
Value="{x:Bind ViewModel.MyValue, Mode=OneWay}"
https://stackoverflow.com/questions/33084142
复制相似问题