Snake Eyes
Snake Eyes

Reputation: 16764

Creating bindable property with Xamarin C# gave No property, bindable property found

I created a class derived from StackLayout:

public class MyStackLayout : StackLayout
{
    private readonly BindableProperty TriggerTypeProperty = BindableProperty.Create("TriggerType", typeof(string), typeof(MyStackLayout), defaultValue: "test", propertyChanged: (b, o, n) => OnTriggerTypePropChanged(b, (string)o, (string)n));

    public string TriggerType
    {
      get
      {
        return (string)GetValue(TriggerTypeProperty);
      }
      set
      {
        SetValue(TriggerTypeProperty, value);
      }
    }

    private static void OnTriggerTypePropChanged(BindableObject bindable, string oldvalue, string newvalue)
    {
      var obj = bindable as MyStackLayout;
      if (obj == null)
      {
        return;
      }

      obj.TriggerType = newvalue;
    }
}

and in XAML:

<controls1:MyStackLayout IsVisible="False" TriggerType="{Binding AStringPropertyFromViewModel}">
   ...
</controls1:MyStackLayout>

I got error:

No property, bindable property, or event found for 'TriggerType', or mismatching type between value and property.

What's wrong ?

If I use in XAML TriggerType="A string" works fine, if I use {Binding ...} won't work.

Upvotes: 2

Views: 1948

Answers (1)

BasisPrune
BasisPrune

Reputation: 94

According to Xamarin docs, here is how to define a bindable property:

public static readonly BindableProperty EventNameProperty =
BindableProperty.Create ("EventName", typeof(string),typeof(EventToCommandBehavior), null);

Try to change your bindable property from private to public to see if it solve your issue.

The link for the documentation : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties

Upvotes: 8

Related Questions