65.9K
CodeProject is changing. Read more.
Home

Expanding a TreeView to a specific node in WPF

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.73/5 (7 votes)

Jan 13, 2010

CPOL
viewsIcon

57643

downloadIcon

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);
        }
    }
}