Published July 30, 2008 | https://doi.org/10.59348/ewvkq-hcr13

Expanding a treeview to a specific node in WPF

Creators & Contributors

I've been exploring the dark alleyways of the Windows Presentation Foundation this week and found no way in my trawlings of the net to expand a treeview to a specific node.

No FindNode, no ExpandTo or ExpandAll. Great.

Anyway, here's the solution I came up with. It relies upon a binding the Tag property to the text you are searching for, but that could obviously be changed.

        /// <summary>
        /// Expand a TreeView to a specific node
        /// </summary>
        /// <param name="tv">The treeview</param>
        /// <param name="node">The string of the node in the Item.Tag property to expand to</param>
        void jumpToFolder(TreeView tv, string node)
        {
            bool done = false;
            ItemCollection ic = tv.Items;

            while (!done)
            {
                bool found = false;

                foreach (TreeViewItem tvi in ic)
                {    
                    if (node.StartsWith(tvi.Tag.ToString()))
                    {
                        found = true;
                        tvi.IsExpanded = true;
                        ic = tvi.Items;
                        if (node == tvi.Tag.ToString()) done = true;
                        break;
                    }
                }

                done = (found == false && done == false);
            }
        }

Expanding a treeview to a specific node in WPF was originally published by Martin Paul Eve at eve.gd: Martin Paul Eve on July 30, 2008.

Files

10.59348_ewvkq-hcr13.pdf

Files (114.7 kB)

Name Size Download all
md5:98b609043434a3ccd8c50214fc755813
114.7 kB Preview Download

Additional details

Description

I've been exploring the dark alleyways of the Windows Presentation Foundation this week and found no way in my trawlings of the net to expand a treeview to a specific node. No FindNode, no ExpandTo or ExpandAll. Great. Anyway, here's the solution I came up with. It relies upon a binding the Tag property to the text you are searching for, but that could obviously be changed.

Identifiers

Dates

Issued
2008-07-30T00:00:00Z
Updated
2008-07-30T11:40:55Z