random
random

Reputation: 487

Xamarin BindableProperty in custom control

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?

Upvotes: 1

Views: 3459

Answers (4)

mhDuke
mhDuke

Reputation: 148

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 view model), it does not - i repeat - work in binding ContentView to its code_behind.cs file.

Upvotes: 2

EvZ
EvZ

Reputation: 12179

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();
    }
}

Upvotes: 4

Kalp
Kalp

Reputation: 1414

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;
}

Upvotes: 0

Libin Joseph
Libin Joseph

Reputation: 7392

In you xaml , do

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

And then in ViewModel, Create a porperty called Id

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

Upvotes: 1

Related Questions