Xamarin形成树视图

时间:2018-01-05 00:33:00

标签: xamarin xamarin.forms treeview

我试图用Xamarin表单中的嵌套数据实现树视图。看起来像这样的东西 enter image description here

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

2 个答案:

答案 0 :(得分:1)

<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>

答案 1 :(得分:0)

是的。这是可能的。我通过将StackPanels嵌套在StackPanels中来实现TreeView。

您可以在此处下载代码: https://github.com/AdaptSolutions/Xamarin.Forms-TreeView

这是一些示例代码:

     private void Render()
        {
            _SpacerBoxView.WidthRequest = IndentWidth;

            if (Children == null || Children.Count == 0)
            {
                SetExpandButtonContent(BlankImagePath);
                return;
            }

            SetExpandButtonContent(IsExpanded ? OpenImagePath : CollapseImagePath);

            foreach (var item in Children)
            {
                item.Render();
            }
        }