Articles in this section
Category / Section

How to expand and collapse TreeView node using image instead expander?

2 mins read

In Xamarin.Forms TreeView, you can expand and collapse TreeViewNode using image instead using the default expander by defining the ExpanderWidth as 0; it hides the default expander icon.

 
<ContentPage xmlns:treeview="clr-namespace:Syncfusion.XForms.TreeView;assembly=Syncfusion.SfTreeView.XForms">
    <ContentPage.BindingContext>
        <local:FileManagerViewModel x:Name="viewModel"></local:FileManagerViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:ExpanderIconVisibilityConverter x:Key="ExpanderIconVisibilityConverter"/>
            <local:ExpanderIconConverter x:Key="ExpanderIconConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <treeview:SfTreeView x:Name="treeView"
                             Indentation="40"
                             ExpanderWidth="0"
                             AutoExpandMode="None"
                             ItemTemplateContextType="Node"
                             ChildPropertyName="SubFiles"
                             ItemsSource="{Binding ImageNodeInfo}">
                          <treeview:SfTreeView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid >
                                <Label Text="{Binding Content.ItemName}"/>
                                <Image Grid.Column="2" Source="{Binding IsExpanded,Converter={StaticResource ExpanderIconConverter}}"
                                       IsVisible="{Binding HasChildNodes,Converter={StaticResource ExpanderIconVisibilityConverter}}">                                        
 
                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="TapGestureRecognizer_ Tapped" />
                                    </Image.GestureRecognizers>
                                </Image>
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>                
            </treeview:SfTreeView.ItemTemplate>
        </treeview: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 GettingStartedBound
{
    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("GettingStartedBound.Icons.expand.png", assembly) : ImageSource.FromResource("GettingStartedBound.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 sample from GitHub

 

Conclusion

I hope you enjoyed learning about how to expand and collapse TreeView node using image instead expander.

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

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

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-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)
Please sign in to leave a comment
Access denied
Access denied