Articles in this section

How to expand and collapse nodes by in .NET MAUI TreeView (SfTreeView)?

In .NET MAUI TreeView, you can expand and collapse TreeViewNode by clicking the image in the ItemTemplate instead using the default expander by defining the ExpanderWidth as 0; it hides the default expander icon.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:syncfusion="clr-namespace:Syncfusion.Maui.TreeView;assembly=Syncfusion.Maui.TreeView"
           xmlns:local="clr-namespace:MauiTreeView"
           x:Class="MauiTreeView.MainPage">

  <ContentPage.BindingContext>
      <local:FileManagerViewModel x:Name="viewModel"/>
  </ContentPage.BindingContext>

  <ContentPage.Resources>
      <ResourceDictionary>
          <local:ExpanderIconVisibilityConverter x:Key="ExpanderIconVisibilityConverter"/>
          <local:ExpanderIconConverter x:Key="ExpanderIconConverter" />
      </ResourceDictionary>
  </ContentPage.Resources>
  <ContentPage.Content>
      <syncfusion:SfTreeView x:Name="treeView"
                             Indentation="40"
                             ItemHeight="40"
                             ExpanderWidth="0"
                             AutoExpandMode="None"
                             ItemTemplateContextType="Node"
                             ChildPropertyName="SubFiles"
                             ItemsSource="{Binding ImageNodeInfo}">
          <syncfusion:SfTreeView.ItemTemplate>
              <DataTemplate>
                  <ViewCell>
                      <ViewCell.View>
                          <Grid x:Name="grid" 
                                Padding="5,5,5,5" 
                                RowSpacing="0" >
                              <Grid.ColumnDefinitions>
                                  <ColumnDefinition Width="40" />
                                  <ColumnDefinition Width="*" />
                                  <ColumnDefinition Width="40" />
                              </Grid.ColumnDefinitions>
                              <Grid Grid.Column="0">
                                  <Image Source="{Binding Content.ImageIcon}"
                                         VerticalOptions="Center" 
                                         HorizontalOptions="Center"
                                         HeightRequest="35" 
                                         WidthRequest="35"/>
                              </Grid>
                              <Grid Grid.Column="1"
                                    RowSpacing="1"
                                    Padding="1,0,0,0"
                                    VerticalOptions="Center">
                                  <Label LineBreakMode="NoWrap" 
                                         TextColor="Black" 
                                         HeightRequest="50"
                                         Text="{Binding Content.ItemName}"
                                         VerticalTextAlignment="Center">
                                      <Label.FontSize>
                                          <OnPlatform x:TypeArguments="x:Double">
                                              <On Platform="Android,iOS">
                                                  <OnIdiom x:TypeArguments="x:Double" Phone="14" Tablet="16" />
                                              </On>
                                              <On Platform="UWP">
                                                  <OnIdiom x:TypeArguments="x:Double" Phone="16" Tablet="18" Desktop="18" />
                                              </On>
                                          </OnPlatform>
                                      </Label.FontSize>
                                  </Label>
                              </Grid>
                              <Image Grid.Column="2" 
                                     Source="{Binding IsExpanded,Converter={StaticResource ExpanderIconConverter}}"
                                     IsVisible="{Binding HasChildNodes,Converter={StaticResource ExpanderIconVisibilityConverter}}"
                                     VerticalOptions="Center" 
                                     HorizontalOptions="Center"
                                     HeightRequest="35" 
                                     WidthRequest="35">
                                  <Image.GestureRecognizers>
                                      <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                                  </Image.GestureRecognizers>
                              </Image>
                          </Grid>
                      </ViewCell.View>
                  </ViewCell>
              </DataTemplate>
          </syncfusion:SfTreeView.ItemTemplate>
      </syncfusion:SfTreeView>
  </ContentPage.Content>
</ContentPage> 

Here, converter is added for the Source and IsVisible properties to change icon for the expanded and collapsed root nodes and change the visibility for root and child nodes.

namespace MauiTreeView
{
   public class ExpanderIconConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           Assembly assembly = typeof(App).GetTypeInfo().Assembly;
           return (bool)value ? ImageSource.FromResource("MauiTreeView.Icons.expand.png", assembly) :     ImageSource.FromResource("MauiTreeView.Icons.collapse.png", assembly);
       
       }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           throw new NotImplementedException();
       }
   }
   public class ExpanderIconVisibilityConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           Assembly assembly = typeof(App).GetTypeInfo().Assembly;
           return (bool)value ? true : false;
       }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           throw new NotImplementedException();
       }
   }
} 

You can expand and collapse node by tapping the image using TapGestureRecognizer.

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
     var imageIcon = sender as Image;
     var treeViewNode = imageIcon.BindingContext as TreeViewNode;
     if (treeViewNode.IsExpanded)
         TreeView.CollapseNode(treeViewNode);
     else
         TreeView.ExpandNode(treeViewNode);
} 

Download the complete sample from GitHub

Conclusion:

I hope you enjoyed learning how to expand and collapse TreeView nodes using an image instead expander in .NET MAUI TreeView.

You can refer to our .NET MAUI TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications.

You can check out our components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

If you have any queries or require clarification, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied