Quantcast
Viewing all articles
Browse latest Browse all 77050

Custom control with complex property in Xaml

Hi,

I have created a cutom control MyControl

public class MyControl : StackLayout
{
    public static readonly BindableProperty LabelControlProperty =
        BindableProperty.Create<MyControl, Label>(p => p.LabelControl, null, BindingMode.OneWayToSource);

    public Label LabelControl
    {
        get { return (Label)GetValue(MyControl.LabelControlProperty); }
        private set { SetValue(MyControl.LabelControlProperty, value); }
    }

    public MyControl() : base()
    {
        LabelControl = new Label();
        this.Children.Add(LabelControl);
    }
}

In this control I have created a readonly property LabelControl which is a Label. This LabelControl is initialized in MyControl's constructor. And I want to have an access to properties of the MyControl.LabelControl property from code and from Xaml.

When I create control instance in code it is working fine

var control = new MyControl();
control.Orientation = StackOrientation.Vertical;
control.LabelControl.Text = "test1";
control.LabelControl.FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label));
(this.Content as StackLayout).Children.Insert(0, control);`

But when I insert MyControl in xaml it is not working

Case 1

<editors:MyControl Orientation="Vertical" LabelControl.Text="test2" LabelControl.FontSize="Medium" />

In this case I get an exception:

Xamarin.Forms.Xaml.XamlParseException was unhandled by user code
HResult=-2146233088
Message=Position 44:49. Type LabelControl not found in xmlns
Source=Xamarin.Forms.Xaml

Case 2

<editors:MyControl Orientation="Vertical">
    <editors:MyControl.LabelControl Text="test2" FontSize="Medium" />
</editors:MyControl>

In this case I don't get an exception, but nothing appears in a page.

Could you help me please? What do I do wrong?
Thanx in advance!

Best Regards,
Aleksandrs


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>