Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms tree view

I'm trying to implement a treeview with nested data in Xamarin forms. Something that looks like this enter image description here

Tapping on each item opens a list of possible sub items. The examples I've seen online only deal with one layer of data using a ListView. Is it possible to implement such hierarchical view of data in xamarin forms.

like image 236
ClueSelecter Avatar asked Jan 05 '18 00:01

ClueSelecter


1 Answers

<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>
like image 56
Akhil George Avatar answered Sep 23 '22 23:09

Akhil George