Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x:Bind to DependencyProperty not working (classic Binding works fine)

I'm having some problems porting my classic bindings to the new compiled bindings in a UWP app.

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-behind file I assign the 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 INotifyPropertyChanged interface correctly implemented):

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

But this compiled binding doesn't:

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

The compiler doesn't give me an error, so it does find the target property when it builds the app, but at runtime it just doesn't work.

I guess I'm missing something really obvious and stupid here, but I just don't know what it is exactly. Thank you in advance for your help!

like image 963
Sergio0694 Avatar asked Oct 12 '15 14:10

Sergio0694


1 Answers

The most annoying difference between the "classic" Binding and the latest compiled binding (x:Bind) is the default binding mode. For classic Binding, the default is OneWay but for x:Bind the default is OneTime, so when you change the property value it doesn't reflect in the UI because the binding just picks up the value once and doesn't care for any future change notifications.

Value="{x:Bind ViewModel.MyValue, Mode=OneWay}"
like image 102
Igor Ralic Avatar answered Nov 16 '22 23:11

Igor Ralic