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"
};
}
}