Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindableProperty in ContentView not working

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.

like image 873
Lennard Fonteijn Avatar asked Oct 12 '16 02:10

Lennard Fonteijn


Video Answer


1 Answers

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;

like image 76
Lennard Fonteijn Avatar answered Oct 11 '22 01:10

Lennard Fonteijn