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).
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.
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://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.