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 customize TreeView ItemTemplate based on the node in Xamarin.Forms (SfTreeView)

Platform: Xamarin.Forms |
Control: SfTreeView

You can customize the TreeView ItemTemplate based on the node by using DataTemplateSelector in Xamarin.Forms.


Binding ItemTemplateSelector to TreeView ItemTemplate.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        <local:FileManagerViewModel x:Name="viewModel"/>
            <local:ItemTemplateSelector x:Key="ItemTemplateSelector" />
        <syncfusion:SfTreeView x:Name="treeView" Indentation="15" AutoExpandMode="AllNodesExpanded" ItemTemplateContextType="Node" 
                               ChildPropertyName="SubFiles" ItemsSource="{Binding ImageNodeInfo}" ItemTemplate="{StaticResource ItemTemplateSelector}"/>


Apply template to node based on SubFiles.

namespace TreeViewXamarin
    public class ItemTemplateSelector : DataTemplateSelector
        public DataTemplate _ParentTemplate { get; set; }
        public DataTemplate _ChildTemplate { get; set; }
        public ItemTemplateSelector()
            this._ParentTemplate = new DataTemplate(typeof(ParentTemplate));
            this._ChildTemplate = new DataTemplate(typeof(ChildTemplate));
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
            var node = item as TreeViewNode;
            var treeviewItem = node.Content as FileManager;
            if (treeviewItem == null)
                return null;
            if (treeviewItem.SubFiles!=null)
                return _ParentTemplate;
                return _ChildTemplate;


TemplateSelector in TreeView

View Sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
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