0
votes

I am using a bindable property in a custom control in order to set a property from the xaml code. However, it seems like my property always will get the default value that I've specified for the bindable property.

My xaml code:

<controls:MyView ID="4" />

My code behind:

public partial class MyView : ContentView
{
    public static readonly BindableProperty IDProperty = BindableProperty.Create(
                                    nameof(ID),
                                    typeof(int),
                                    typeof(MyView),
                                    15);
    public int ID
    {
        get
        {
             return (int)GetValue(IDProperty);
        }
        set
        {
            SetValue(IDProperty, value);
        }
    }

    private MyViewViewModel viewModel;

    public MyView()
    {
        InitializeComponent();
        viewModel = new MyViewViewModel() {};
        BindingContext = viewModel; 
    }
}

I expect that my property should get value 4 in this example, but it always get the default value 15. Should the property be set in the constructor or later?

What am I doing wrong?

4
Can you share all the code of your control and especially constructor? Maybe you set the BindingContext incorrectly ...EvZ
@EvZ I've updated the code above.random
Why do you have a ViewModel embedded into your CustomControl?EvZ
It's not working because the BindingContext is pointing to a MyViewViewModel object. If you want to make it works change BindingContext = viewModel; by BindingContext = this. BTW, the MyViewViewModel field is not necessary. I hope it helps.dbvega

4 Answers

2
votes

Why do you embed a ViewModel inside your custom control?

It is weird and even wrong. The idea behind a custom control is that you could reuse and bind it to the parent's ViewModel. Think of a simple Button control, it is reusable by simple placing it on the screen and setting the BindableProperties like Text, Command and etc. It is working because it's BindingContext by default is the same as it's parent.

In your case you sort of isolate your control from any modifications, since you set the BindingContext to a private custom ViewModel class. You have to rethink your solution.

It should be as simple as:

public partial class MyView : ContentView
{
    public static readonly BindableProperty IDProperty = BindableProperty.Create(
                                    nameof(ID),
                                    typeof(int),
                                    typeof(MyView),
                                    15);
    public int ID
    {
        get => (int)GetValue(IDProperty);
        set => SetValue(IDProperty, value);
    }

    public MyView()
    {
        InitializeComponent();
    }
}
1
votes

You don't need Bindable property if your are not binding , Just Create a Normal Property of type int With ID as property name.And then you can assign the ID from XAML.(Intellisense will also show the ID property)

public int ID
{
    get;set;
}
1
votes

I see it's too late, but i have been suffering for a while now, that - for a reason that i don't know - a ContentView(Custom view element) won't bind when you set it's BindingContext property in any way other than this:

<ContentView x:Class="mynamespace.CustomViews.MyView"
....
 x:Name="this">

then on the main container element (in my case a frame) set the BindingContext

<Frame BindingContext="{x:Reference this}"
....>

Setting the BindingContext in the constructor - in MyView.xaml.cs - does not work, while this way - and other ways - work in binding a View to another class (a ViewModel mainly), it does not - i repeat - work in binding ContentView to it's codeBehind.cs file.

0
votes

In you xaml , do

<controls:MyView ID="{Binding Id}" />

And then in ViewModel, Create a porperty called Id

public int Id {get; set;} = 4;