9
votes

I made a very simple Yes/No RadioBox control.

In it's code behind I have the following BindableProperty:

    public static readonly BindableProperty SelectedValueProperty = BindableProperty.Create(
        "SelectedValue",
        typeof(int),
        typeof(YesNoView),
        0,
        BindingMode.TwoWay);

And it's property:

    public int SelectedValue
    {
        get { return (int) GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

This property is bound in the ContentView-XAML using something like this:

          <DataTrigger Binding="{Binding SelectedValue}" Value="0" TargetType="Image">
            <Setter Property="Source" Value="radiobutton_checked.png" />
          </DataTrigger>

Whenever I use my ContentView in some Page and set the SelectedValue to a constant value, everything works fine!

However, when I use a {Binding SomeValue} instead of a constant value, where SomeValue is a property (with notify) on the Page I'm using the ContentView on, it will simply refuse to do anything. Whenever I set SomeValue to some value, it never reaches the ContentView and it's driving me insane why it isn't working.

Even when I adjust the BindableProperty to also specify a propertyChanged event, it is simply never triggered unless I revert back to a constant value in the Xaml, rather than a binding.

Could anyone shed a light as to why this is happening and not working as I would expect?

EDIT: I should add that in both the Page and the View the BindingContext is set to this.

1

1 Answers

36
votes

I figured it out after a total of 4 hours wasted...

Never do BindingContext = this; in your ContentView, as it hijacks the context from the Page trying to databind to it. Instead use Content.BindingContext = this;