Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to bind a object in a custom Control

Hi there community,
I've got a Problem with Databinding in Xamarin.
So I have this Custom Control which already has a bindable Property SelectedItem:
` public LTVLI SelectedItem
{
get => (LTVLI)GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}

    public static readonly BindableProperty SelectedItemProperty =
        BindableProperty.CreateAttached(
            nameof(SelectedItem),
            typeof(LTVLI),
            typeof(MyPicker),
            null,
            propertyChanged: OnSelectedItemChanged
            );
    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if (bindable is MyPicker picker)
            picker.OnPropertyChanged(nameof(picker.SelectedItem));
    }

I want to bind the property Linie.Typ:
<controls:CustomControl SelectedItem="{Binding Linie.Typ}"/>

I already set the BindingContext in the Constructor:
BindingContext = this;

and Typ is of Type "SwitchItem", which I directly derived from LTVLI.
I tested with an Alert if the Property is bound, but no positive result.

Does anyone of you got an Idea what I did wrong?

Tagged:

Best Answer

  • HHHChristHHHChrist Member
    Accepted Answer

    So I could solve my Problem.
    I removed the BindingContext from the Code Behind, set
    x:Name=ViewName
    to the View and added
    Source = {x:Reference ViewName}
    to each Binding. Not what I wanted but it works as intended.

Answers

  • HHHChristHHHChrist Member
    Accepted Answer

    So I could solve my Problem.
    I removed the BindingContext from the Code Behind, set
    x:Name=ViewName
    to the View and added
    Source = {x:Reference ViewName}
    to each Binding. Not what I wanted but it works as intended.

Sign In or Register to comment.