0

Im trying to build my app by having a page that is filled with reusable controls. I want to bind objects to the custom controls at runtime in order to fill their properties depending on the page there on. So far what I've tried is not working correctly and I'm wondering if there is a better approach to this. The current error that I'm getting says that it cannot convert Binding type to String. Here is one example of what I've tried so far.

View Model:

public class MenuItem
    {
        public Page pageTo { get; set; } 
        public string title { get; set; } 
        public string subtitle { get; set; } 
        public string iconPath { get; set; } 
    }

    public class VMMenuItem : ContentView
    {
        public MenuItem item { get; set; } 

    public static BindableProperty ItemProperty = BindableProperty.Create("Item", typeof(MenuItem), typeof(VMMenuItem), null, BindingMode.TwoWay, );

    public VMMenuItem()
    {
        var menuTapped = new TapGestureRecognizer();


        StackLayout Main = new StackLayout

        {
            BindingContext = item,
            Children = {

                new SectionLine(),
                new StackLayout
                {

                    Padding = new Thickness(10),
                    Orientation = StackOrientation.Horizontal,
                    HorizontalOptions = LayoutOptions.Fill,
                    Children = {
                        new Label {

                            Margin = new Thickness(10, 2, 0, 0),
                            HorizontalOptions = LayoutOptions.StartAndExpand,
                            Text = "{Binding title}"
                        },
                        new Label
                        {

                            Margin = new Thickness(10, 2, 10, 0),
                            FontSize = 14,
                            TextColor = Color.FromHex("#c1c1c1"),
                            HorizontalOptions = LayoutOptions.End,
                            Text = "{Binding subtitle}"
                        },
                        new Image {

                            HorizontalOptions = LayoutOptions.End,
                            Source = "{Binding iconPath}",
                            WidthRequest = 20
                        }
                    }
                }
            }
        };

        Main.GestureRecognizers.Add(menuTapped);
        Content = Main;
    }

    public MenuItem menuItem
    {
        get { return (MenuItem)GetValue(ItemProperty); } 
        set { SetValue(ItemProperty, value); } 
    }
}

Page Front End:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-namespace:TamarianApp;assembly=TamarianApp" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TamarianApp.RugPage">
    <Grid>

        <local:VMMenuItem menuItem="{Binding lengthMenuItem}"></local:VMMenuItem>

    </Grid>
</ContentPage>

Page Backend:

public partial class RugPage : ContentPage
    {

        MenuItem lengthMenuItem; 
        public RugPage()
        {
            InitializeComponent();

            App.currentPage = this;
            BindingContext = App.rug;
            lengthMenuItem = new MenuItem
            {
                title = "length",
                iconPath = "icons/blue/next",
                subtitle = "somelength"
            };
        }
   }

1 Answer 1

1

You need to use a bindable property here is an example of how to use them:

public static readonly BindableProperty ImageProperty = 
    BindableProperty.Create(nameof(Image), typeof(ImageSource), typeof(DashEntry), null); 
public ImageSource Image
{
    get
    {
        return (ImageSource)GetValue(ImageProperty);
    }
    set
    {
        SetValue(ImageProperty, value);
    }
}

And the documentation about it:

https://developer.xamarin.com/guides/xamarin-forms/xaml/bindable-properties/

Sign up to request clarification or add additional context in comments.

1 Comment

Thats what I've been trying to use but it keeps giving me an error saying it cannot convert type Binding to type System.string. Its getting tripped up when I set a property using binding like: <Label someCustomProperty="{Binding someString}". Have you ever seen this happen to you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.