Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom controls with properties

I need to create a custom control with properties.

This is my custom control:

public class ExpandableStackLayout : StackLayout
{
    public String Title{get;set;}

    public ContentView View { set; get; }

    public String Image{set;get;}


    public ExpandableStackLayout(string title, string image, ContentView view)
    {
        Title = title;
        Image = image;
        View = view;
    }
}

But when I try used it in xaml says : "No default constructor find" and if i create the default constructor call this and not call constructor with parameters.

This is my xaml:

<control:ExpandableStackLayout Title="Title" Image="imag.png">
            <control:ExpandableStackLayout.View>
                <ContentView>
                    <OtherView/>
                </ContentView>
            </control:ExpandableStackLayout.View>
        </control:ExpandableStackLayout>

UPDATE

I tried with your tips and i have others problems:

public static readonly BindableProperty TitleProperty = BindableProperty.Create(
        propertyName: "Title",
        returnType: typeof(String),
        declaringType: typeof(String),
        defaultValue: String.Empty);

    public String Title
    {
        get => (String)GetValue(TitleProperty); 
        set => SetValue(TitleProperty, value); 
    }

    public ContentView View { set; get; }

    public static readonly BindableProperty ImageProperty = BindableProperty.Create(
        propertyName: "Image",
        returnType: typeof(String),
        declaringType: typeof(String),
        defaultValue: string.Empty);

    public String Image
    {
        get => (String)GetValue(TitleProperty);
        set => SetValue(ImageProperty, value);
    }

    public ExpandableStackLayout()
    {
        //Do somethings whith parameters but are null
        usemyParameters();
    }
like image 997
Miguel Angel Muñoz Avatar asked Dec 11 '25 13:12

Miguel Angel Muñoz


1 Answers

There is no need to create constructor. Here is the solution how to implement Title and Image properties.

public class ExpandableStackLayout : StackLayout
{
    public static readonly BindableProperty TitleProperty =
        BindableProperty.Create<ExpandableStackLayout, string>(p => p.Title, string.Empty, BindingMode.TwoWay);

    public string Title
    {
        get { return (string)base.GetValue(TitleProperty); }
        set { base.SetValue(TitleProperty, value); }
    }

    public static readonly BindableProperty ImageProperty =
        BindableProperty.Create<ExpandableStackLayout, string>(p => p.Image, string.Empty, BindingMode.TwoWay);

    public string Image
    {
        get { return (string)base.GetValue(ImageProperty); }
        set { base.SetValue(ImageProperty, value); }
    }
}

I don't know what you want to achieve with the View. As a start I recommend you to study these controls and Stacklayout implementation.

like image 167
Jan Nepraš Avatar answered Dec 14 '25 19:12

Jan Nepraš