I have a menu that has multiple menu items. Each menu item has a title , subtitle, and a page property that needs to be bound to. But when binding to the UI control I get errors saying that binding cannot convert to MenuItem. Even when testing if I can bind a string to a bindable property it says I cannot convert binding to type string. What is going on? Am I doing this correctly?
Xaml:
`<local:MenuItemControl x:Name="scanMenuItem" Item="{Binding scanItem}"></local:MenuItemControl>`
`<local:MenuItemControl x:Name="factoryMenuItem" Item="{Binding factoryItem}"></local:MenuItemControl>`
Code Behind:
public partial class Home : ContentPage
{
public string s { get; set; }
public MenuItem scanItem;
public MenuItem factoryItem;
public Home()
{
InitializeComponent();
scanItem = new MenuItem("Scan", "", new Scan());
factoryItem = new MenuItem("Factory", "", new FactoryPage());
NavigationPage.SetHasNavigationBar(this, true);
Title = "Home";
}
}
MenuItem custom UI control:
public class MenuItemControl : ContentView
{
public static BindableProperty item = BindableProperty.Create("Item", typeof(MenuItem), typeof(MenuItemControl), null, BindingMode.TwoWay);
public MenuItem Item
{
get
{
return (MenuItem)GetValue(item);
}
set
{
SetValue(item, value);
}
}
public MenuItemControl()
{
if (Item == null){
Item = new MenuItem("", "", null);
}
BoxView line = new BoxView
{
HeightRequest = 1,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.FromHex("#f1f1f1")
};
Label title = new Label
{
Text = Item.title,
Margin = new Thickness(10, 2, 0, 0),
HorizontalOptions = LayoutOptions.StartAndExpand,
};
Label subTitle = new Label
{
Text = Item.subtitle,
Margin = new Thickness(10, 2, 10, 0),
FontSize = 14,
TextColor = Color.FromHex("#c1c1c1"),
HorizontalOptions = LayoutOptions.End,
};
Image image = new Image
{
HorizontalOptions = LayoutOptions.End,
Source = "icons/blue/next",
WidthRequest = 20
};
new StackLayout
{
Children = {
line,
new StackLayout{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Fill,
Padding = new Thickness(10),
Children ={
title,
subTitle,
image
}
}
}
};
}