Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-find-a-treeviewitem-in-a-treeview.md
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

3.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Find a TreeViewItem in a TreeView 03/30/2017
csharp
vb
TreeView control [WPF], finding a TreeViewItem
TreeViewItem [WPF], finding
72ecd40c-3939-4e01-b617-5e9daa6074d9

How to: Find a TreeViewItem in a TreeView

The xref:System.Windows.Controls.TreeView control provides a convenient way to display hierarchical data. If your xref:System.Windows.Controls.TreeView is bound to a data source, the xref:System.Windows.Controls.TreeView.SelectedItem%2A property provides a convenient way for you to quickly retrieve the selected data object. It is typically best to work with the underlying data object, but sometimes you may need to programmatically manipulate the data's containing xref:System.Windows.Controls.TreeViewItem. For example, you may need to programmatically expand the xref:System.Windows.Controls.TreeViewItem, or select a different item in the xref:System.Windows.Controls.TreeView.

To find a xref:System.Windows.Controls.TreeViewItem that contains a specific data object, you must traverse each level of the xref:System.Windows.Controls.TreeView. The items in a xref:System.Windows.Controls.TreeView can also be virtualized to improve performance. In the case where items might be virtualized, you also must realize a xref:System.Windows.Controls.TreeViewItem to check whether it contains the data object.

Example

Description

The following example searches a xref:System.Windows.Controls.TreeView for a specific object and returns the object's containing xref:System.Windows.Controls.TreeViewItem. The example ensures that each xref:System.Windows.Controls.TreeViewItem is instantiated so that its child items can be searched. This example also works if the xref:System.Windows.Controls.TreeView does not use virtualized items.

Note

The following example works for any xref:System.Windows.Controls.TreeView, regardless of the underlying data model, and searches every xref:System.Windows.Controls.TreeViewItem until the object is found. Another technique that has better performance is to search the data model for the specified object, keep track of its location within the data hierarchy, and then find the corresponding xref:System.Windows.Controls.TreeViewItem in the xref:System.Windows.Controls.TreeView. However, the technique that has better performance requires knowledge of the data model and cannot be generalized for any given xref:System.Windows.Controls.TreeView.

Code

[!code-csharpTreeViewFindTVI#1] [!code-vbTreeViewFindTVI#1]

The previous code relies on a custom xref:System.Windows.Controls.VirtualizingStackPanel that exposes a method named BringIntoView. The following code defines the custom xref:System.Windows.Controls.VirtualizingStackPanel.

[!code-csharpTreeViewFindTVI#2] [!code-vbTreeViewFindTVI#2]

The following XAML shows how to create a xref:System.Windows.Controls.TreeView that uses the custom xref:System.Windows.Controls.VirtualizingStackPanel.

[!code-xamlTreeViewFindTVI#3]

See also