We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to expand and collapse TreeView node using image instead expander

Platform: Xamarin.Forms |
Control: SfTreeView

In 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

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile