如何在xamarin.forms MasterDetailPage中单击MenuItems添加子菜单项

时间:2017-05-18 08:09:24

标签: xamarin xamarin.ios xamarin.android xamarin.forms master-detail

您好我正在尝试添加子菜单项当点击MasterDatailPage中的Menuitems时。我的主页xaml就像

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="HomeMaster"
             Title="Master"
             Icon="hamburger.png">
  <StackLayout>
        <ListView x:Name="ListViewMenuItems"
              SeparatorVisibility="None"
              HasUnevenRows="true"
              ItemTapped="ListViewMenuItems_ItemTapped"
              ItemsSource="{Binding MenuItems}">
            <ListView.Header>
                <Grid BackgroundColor="Transparent">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="10"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="10"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="10"/>
                    </Grid.RowDefinitions>
                    <Image
              Grid.Column="1"
              Grid.Row="2"
              Source="User.png" HeightRequest="100" x:Name="userImg"/>
                </Grid>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <StackLayout VerticalOptions="FillAndExpand"
                             Orientation="Horizontal"
                             Padding="20,10,0,10"
                             Spacing="20">

                            <Image Source="{Binding Icon}"
                         WidthRequest="40"
                         HeightRequest="40"
                         VerticalOptions="Center" />

                            <Label Text="{Binding Title}"
                         FontSize="Medium"
                         VerticalOptions="Center"
                         TextColor="Black"/>
                        </StackLayout>
                            <StackLayout Orientation="Vertical" Margin="10" Padding="20,10, 0, 0" IsVisible="{Binding IsExtraControlsVisible}">
                            <Label Text="Test Definition" FontSize="Medium" VerticalOptions="Center" TextColor="Black"/>
                            <Label Text="Maptest" FontSize="Medium" VerticalOptions="Center" TextColor="Black"/>
                         </StackLayout>
                       </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>
    </StackLayout>
</ContentPage>

在我的.cs文件中,ItemTapped就像

private void ListViewMenuItems_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var item = e.Item as MRPHomeMenuItem;
            if (item.Id == 0)
            {
            item.IsExtraControlsVisible = true;
            }
        }

当我点击子菜单项时,总单元格正在调用,因为我给出了堆栈布局viewcell。如果在单击this等菜单项时在单独的单元格中添加这些子菜单项

1 个答案:

答案 0 :(得分:0)

您需要的是可展开的ListView,为实现这一目标,我们需要对列表中的项目进行分组,这意味着要实现GroupHeaderTemplate的{​​{1}}。

您可以在此处查看博客:

Xamarin.Forms Expandable ListView

Expandable ListView in Xamarin Forms using Grouping