X: DependencyProperty binding does not work (classic Binding works fine)

I'm having trouble migrating my classic bindings to new compiled bindings in a UWP application.

I have a UserControl with a simple 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
}

In my page code file, I assign a DataContext and create a parameter for the compiled binding:

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

public MyPageViewModel ViewModel => (MyPageViewModel)DataContext;

Now this classic binding works (the target parameter has the correct INotifyPropertyChanged interface):

<controls:MyUserControl Value="{Binding MyValue}"/>

But this compiled binding is not :

<controls:MyUserControl Value="{x:Bind ViewModel.MyValue}"/>

The compiler does not give me an error, so it finds the target property when creating the application, but at runtime it just doesn't work.

, - , , . !

+4
1

"" (x: Bind) - . OneWay, x: OneTime, , , , .

Value="{x:Bind ViewModel.MyValue, Mode=OneWay}"
+6

Source: https://habr.com/ru/post/1611300/


All Articles