Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Expanding a TreeView to a specific node in WPF

0.00/5 (No votes)
13 Jan 2010 1  
Sometimes ago , Ive been looking for a method to expand a TreeView to a specific node in WPF.I couldn't find anything useful and eventually I have written my own :/// /// Expand a TreeView to a specific node/// /// Searching will begin...
Sometimes ago , Ive been looking for a method to expand a TreeView to a specific node in WPF.
I couldn't find anything useful and eventually I have written my own :

/// <summary>
/// Expand a TreeView to a specific node
/// </summary>
/// <param name="TreeViewItem">Searching will begin from this TreeViewItem</param>
/// <param name="NodeName">the name of the target node</param>
void JumpToNode(TreeViewItem tvi, string NodeName)
{
    if (tvi.Name == NodeName)
    {
        tvi.IsExpanded = true;
        tvi.BringIntoView();
        return;
    }
    else
        tvi.IsExpanded = false;

    if (tvi.HasItems)
    {
        foreach (var item in tvi.Items)
        {
            TreeViewItem temp = item as TreeViewItem;
            JumpToNode(temp, NodeName);
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here