我正在尝试在xamarin表单上尝试动态添加菜单和子菜单。我能够成功绑定所有主菜单。现在我试图添加一个子菜单,我看到一些文章如何添加一个子菜单,我知道我应该使用分组,分组的问题是它将在每个主菜单中至少添加一个子菜单。因此,我想创建一个标签(如果存在子菜单),然后将其追加到标签上。怎么做?
标签位于ListView.ItemTemplate内部。在主菜单之后,如果有子菜单,我需要在标签中进行绑定。
我的XAML文件
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" x:Name="submenu" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand" VerticalTextAlignment="Center" Text="{Binding Title}" FontSize="24"/>
//Here i need to append label if it has submenu
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
C#代码
public ObservableCollection<LayoutPageMenuItem> MenuItems { get; set; }
List<LayoutPageMenuItem> layoutPageMenuItems = new List<LayoutPageMenuItem>();
for (int i = 0; i < Menus.AllMenus.Count; i++)
{
var layoutPageMenuItem = new LayoutPageMenuItem();
if (Menus.AllMenus[i].IsSubMenu == false && Menus.AllMenus[i].ParentMenuId == "" || Menus.AllMenus[i].ParentMenuId == null)
{
layoutPageMenuItem.Id = Menus.AllMenus[i].MenuId;
layoutPageMenuItem.Title = Menus.AllMenus[i].MenuName;
layoutPageMenuItems.Add(layoutPageMenuItem);
}
else
{
var Submenu = new Label { Text = Menus.AllMenus[i].MenuName };
}
}
MenuItems = new ObservableCollection<LayoutPageMenuItem>(layoutPageMenuItems);