Reputation: 740
I have made a custom control which has a property X
- which hides the VisualElement.X
property of parent.
public class MyCustomControl : ContentView // is a distant child of VisualElement
{
public new double X
{
get { return 0; }
set { Console.WriteLine("I was not called with: " + value); }
}
}
I set the X
of the custom control in xaml:
<controls:MyCustomControl X="10" />
But here the Xamarin.Forms.VisualElement.X
property setter is called instead of the MyCustomControl.X
setter. Why? And how can I make it so my custom control property is used instead?
As a side note. When x:Name="myCustomControl
and myCustomControl.X = 10
in code behind - then the setter of MyCustomControl
is called.
When declaring property that does not exist in parent:
public double AnotherX
{
get { return 0; }
set { Console.WriteLine("Was called with: " + value); }
}
the setter is called. (From xaml).
Upvotes: 1
Views: 392
Reputation: 1342
you create a bindableproperty not just a property see below on how to make a bindable property
private readonly BindableProperty CustomXProperty = BindableProperty.Create(nameof(CustomX), typeof(double), typeof(MyCustomControl), defaultValue: 0.0);
public double CustomX
{
get
{
return (double)GetValue(CustomXProperty);
}
set
{
SetValue(CustomXProperty, value);
}
}
Please see here for more information https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties
Upvotes: 1
Reputation: 164
This is because you are setting the BindableProperty 'X' of the VisualElement through the Xaml. It should work if you create a BindableProperty 'X' in your custom control as well.
Upvotes: 3