Click here to Skip to main content
15,885,028 members
Articles / Desktop Programming / WPF
Tip/Trick

Expanding a TreeView to a specific node in WPF

Rate me:
Please Sign up or sign in to vote.
4.73/5 (7 votes)
13 Jan 2010CPOL 56K   4   6
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSalam Pin
behrang136030-Jul-12 3:02
behrang136030-Jul-12 3:02 
GeneralReason for my vote of 4 helpfull.. Pin
Mir Anas8-Aug-11 19:25
Mir Anas8-Aug-11 19:25 
GeneralReason for my vote of 4 it's helpful! thanks! Pin
liwenjiede29-Dec-10 20:37
liwenjiede29-Dec-10 20:37 
GeneralClose Nodes Pin
AspDotNetDev18-Jan-10 15:06
protectorAspDotNetDev18-Jan-10 15:06 
GeneralRe: Close Nodes Pin
Mohammad Dayyan18-Jan-10 20:28
Mohammad Dayyan18-Jan-10 20:28 
GeneralRe: Close Nodes Pin
flobadob197524-Oct-14 3:07
flobadob197524-Oct-14 3:07 
There is also ExpandSubtree if you just want to expand something, and not close other things

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.