Quite often TreeView is used in scenarios where a particular item should be displayed on the screen. You may want to display some item when the tree is initially shown on the screen or when a new item is added. These scenarios look simple but are a bit tricky because you have to wait until the TreeView loads and then make your desired actions.

Waiting for Loaded Event

TreeView provides the BringIntoView functionality in several forms: you can call BringIntoView from the tree itself and bring a child item; get some TreeViewItem by calling GetItemByPath and call BringIntoView upon this item container; or use the BringIndexIntoView alternatives of these methods. With all of these approaches you had to wait until the TreeView is loaded and perform their call after that. In 2010 Q3 SP1 release a new method is added to the TreeView called BringPathIntoView.

BringPathIntoView Method

This method brings the required item into view by making a recursive traversing of the hierarchy. It will wait until the TreeView is loaded and execute in the appropriate moment. You should provide an exact path to the item and instruct the TreeView which property to be used to match the provided path by setting TextSearch.TextPath attached property.

 

<telerik:RadTreeView x:Name="myTreeView"
    ...
    telerik:TextSearch.TextPath="Name"
    />

 

So if you have code similar to the following snippet in the page constructor or somewhere else where the TreeView isn't loaded completely you won't be able to bring the desired item.

 

public MainPage()
{
    InitializeComponent();
 
    ...
 
    RadTreeViewItem itemContainer = myTreeView.GetItemByPath(path) as RadTreeViewItem;
    if (itemContainer != null)
    {
        itemContainer.BringIntoView();
    }
}

 

Instead, you could simply call BringPathIntoView and the method will handle the timing for you automatically.

 

public MainPage()
{
    InitializeComponent();
 
    ...
     
    myTreeView.BringPathIntoView(path);
}

 

Adding and showing new item could also be performed using this method.

TreeView BringIntoView

 

You can download the sample application from here.

Telerik TreeView for Silverlight

Telerik TreeView for WPF

And download a trial version of Telerik RadControls from here:

RadControls for Silverlight - Download Trial

RadControls for WPF - Download Trial


About the Author

Hristo Milyakov

 is Software Developer in Telerik XAML Team

Related Posts

Comments

Comments are disabled in preview mode.