Select [and highlight] a node programmatically in SfTreeView

Hi,

I have an SfTreeView bound to my ViewModel:



My requirement is that when tapping on a parent node (that has child nodes in it), it should select that node instead of the parent. 
So for example, in this case, when I tap on the Referral item, it should really highlight and select the Referral Details instead. 

Thanks!


1 Reply 1 reply marked as answer

LN Lakshmi Natarajan Syncfusion Team April 7, 2021 05:34 AM UTC

Hi Amir, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “When tapping on a parent node, it should select the child node instead of the parent” from our side. We would like to inform you that you can achieve your requirement by programmatically setting the SelectedItem for SfTreeView. Please refer to the following code snippets to achieve your requirement, 
 
Behavior: Hook ItemTapped event, and set the SelectedItem for as the child node. 
public class TreeViewBehavior : Behavior<SfTreeView> 
{ 
    SfTreeView TreeView; 
    protected override void OnAttachedTo(SfTreeView bindable) 
    { 
        base.OnAttachedTo(bindable); 
 
        TreeView = bindable; 
        TreeView.ItemTapped += TreeView_ItemTapped; 
    } 
 
    private void TreeView_ItemTapped(object sender, Syncfusion.XForms.TreeView.ItemTappedEventArgs e) 
    { 
        var selectedNode = e.Node as TreeViewNode; 
        if (selectedNode.ChildNodes != null && selectedNode.ChildNodes.Count > 0) 
        { 
            TreeView.SelectedItem = selectedNode.ChildNodes[0]; 
            e.Handled = true; 
        } 
    } 
} 
 
Please refer to our user guidance document regarding programmatic selection in the following link, 
 
We have prepared a sample and attached in the following link, 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 


Marked as answer
Loader.
Up arrow icon