Xamarin形成树视图

我正在尝试使用Xamarin表单中的嵌套数据实现树视图.看起来像这样的东西

《Xamarin形成树视图》

点击每个项目将打开可能的子项列表.我在网上看到的例子只使用ListView处理一层数据.是否有可能以xamarin形式实现这种分层数据视图.

最佳答案

<ListView x:Name="GroupedView" GroupDisplayBinding="{Binding Header}" RowHeight="75" IsGroupingEnabled="True" HasUnevenRows="True">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout HeightRequest="{Binding height}">
          <Label Text="{Binding MenuText}"  TextColor="Green" >
            <Label.GestureRecognizers>
              <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
            </Label.GestureRecognizers>
          </Label>
          <ScrollView>
            <ListView ItemsSource="{Binding submenuitems}"  HasUnevenRows="True" IsVisible="{Binding expand}">
              <ListView.ItemTemplate>
                <DataTemplate>
                  <ViewCell>
                    <StackLayout Margin="25,5,5,5" HeightRequest="{Binding height}">
                      <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
                      </StackLayout.GestureRecognizers>                      
                      <Label Text="{Binding MenuText}"  TextColor="DarkBlue"/>
                      <ScrollView>
                        <ListView ItemsSource="{Binding submenuitems_2}" HasUnevenRows="True" IsVisible="{Binding expand}">
                          <ListView.ItemTemplate>
                            <DataTemplate>
                              <ViewCell >
                                <StackLayout Margin="35,10,5,5" HeightRequest="45">
                                  <Label Text="{Binding MenuText}"  TextColor="Red"/>
                                </StackLayout>
                              </ViewCell>
                            </DataTemplate>
                          </ListView.ItemTemplate>
                        </ListView>
                      </ScrollView>
                    </StackLayout>
                  </ViewCell>
                </DataTemplate>
              </ListView.ItemTemplate>
            </ListView>
          </ScrollView>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
  <ListView.GroupHeaderTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout Orientation="Horizontal" Padding="5,5,5,5" BackgroundColor="#E2F5F9">
          <Button Image="{Binding StateIcon}" BackgroundColor="Transparent" BorderColor="Transparent" BorderWidth="0" Clicked="HeaderTapped" CommandParameter="{Binding .}"/>
          <Label Text="{Binding Header}" TextColor="#005569" FontSize="15" VerticalOptions="Center"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.GroupHeaderTemplate>
</ListView>
点赞