New to Telerik UI for WPF? Download free 30-day trial

Get Item by Path

The RadTreeView API offers you the ability to get an item by path programmatically. This tutorial will show you how to do that.

Using GetItemByPath in static RadTreeView

Here is an ordinary treeview declaration:

        <telerik:RadTreeView TextSearch.TextPath="Header"> 
            <telerik:RadTreeViewItem Header="Sport Categories"> 
                <telerik:RadTreeViewItem Header="Football"> 
                    <telerik:RadTreeViewItem Header="Futsal" /> 
                    <telerik:RadTreeViewItem Header="Soccer" /> 
                </telerik:RadTreeViewItem> 
                <telerik:RadTreeViewItem Header="Tennis"> 
                    <telerik:RadTreeViewItem Header="Court" /> 
                    <telerik:RadTreeViewItem Header="Table" /> 
                </telerik:RadTreeViewItem> 
                <telerik:RadTreeViewItem Header="Cycling"> 
                    <telerik:RadTreeViewItem Header="Road" /> 
                    <telerik:RadTreeViewItem Header="Mountain" /> 
                </telerik:RadTreeViewItem> 
            </telerik:RadTreeViewItem> 
        </telerik:RadTreeView> 

WPF RadTreeView Sample Declaration

For example, see the following situation - you want to retrieve the treeview item with Header Soccer. In order to do that you need to perform the following steps:

  1. Create a path, including only the treeview items' header and using some separator.

  2. Set the telerik:TextSearch.TextPath attached property to indicate which property to be used as a path segment.

        <telerik:RadTreeView TextSearch.TextPath="Header" /> 
    
  3. Invoke the GetItemByPath() method of the RadTreeView class.

        private void GetTreeViewItemByPath() 
        { 
            string path = "Sport Categories|Football|Soccer"; 
            RadTreeViewItem targetItem = radTreeView.GetItemByPath( path, "|" ); 
        } 
    
        Private Sub GetTreeViewItemByPath() 
            Dim path As String = "Sport Categories|Football|Soccer" 
            Dim targetItem As RadTreeViewItem = radTreeView.GetItemByPath(path, "|") 
        End Sub 
    

    The GetItemByPath() method will work only after the treeview has loaded. It will not work before that.

        this.radTreeView.Loaded += new RoutedEventHandler( radTreeView_Loaded ); 
        private void radTreeView_Loaded( object sender, RoutedEventArgs e ) 
        { 
            // If the treeview is data bound, use the 
            // GetItemByPath method here - when the treeview has already loaded. 
        } 
    
        AddHandler Me.radTreeView.Loaded, AddressOf radTreeView_Loaded 
        Private Sub radTreeView_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 
            ' If the treeview is data bound, use the ' 
            ' GetItemByPath method here - when the treeview has already loaded. ' 
        End Sub 
    

    Note that, invoking the GetItemByPath() method will expand the target item without animation, regardless the value of the AnimationManager.IsAnimationEnabled property.

The RadTreeView class supports PathSeparator property, which represents the default separator. The default separator for the RadTreeView is "\", but it can be changed. Here you can see how the previous example will look like if you set the PathSeparator property.

private void GetTreeViewItemByPath() 
{ 
    string path = "Sport Categories|Football|Soccer"; 
    radTreeView.PathSeparator = "|"; 
    RadTreeViewItem targetItem = radTreeView.GetItemByPath( path ); 
} 
Private Sub GetTreeViewItemByPath() 
    Dim path As String = "Sport Categories|Football|Soccer" 
    radTreeView.PathSeparator = "|" 
    Dim targetItem As RadTreeViewItem = radTreeView.GetItemByPath(path) 
End Sub 

The RadTreeView has ExpandItemByPath() method which internally uses the GetItemByPath(), and in addition expands the item

Using GetItemByPath in a Data Bound RadTreeView

You can see how to use the GetItemByPath() method with a tree in a data binding scenario in the Expand and Select Item with Load on Demand Enabled RadTreeView article.

See Also

In this article