Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-use-selectedvalue-selectedvaluepath-and-selecteditem.md
T
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.7 KiB

title, description, ms.date, helpviewer_keywords, ms.assetid
title description ms.date helpviewer_keywords ms.assetid
How to: Use SelectedValue, SelectedValuePath, and SelectedItem Learn how to use the SelectedValue and SelectedValuePath properties to specify a value for the SelectedItem of a Windows Presentation Foundation TreeView. 03/30/2017
TreeView control [WPF], SelectedValue properties
Control class [WPF], SelectedItem properties
Control class [WPF], TreeView properties
Control class [WPF], SelectedValue properties
TreeView control [WPF], SelectedItem properties
SelectedValue [WPF], SelectedValuePath properties
TreeView control [WPF], SelectedValuePath properties
Control class [WPF], SelectedValuePath properties
SelectedValue [WPF], SelectedItem properties
2fc92ad4-f02c-4f89-bbe9-d4978a7af0db

How to: Use SelectedValue, SelectedValuePath, and SelectedItem

This example shows how to use the xref:System.Windows.Controls.TreeView.SelectedValue%2A and xref:System.Windows.Controls.TreeView.SelectedValuePath%2A properties to specify a value for the xref:System.Windows.Controls.TreeView.SelectedItem%2A of a xref:System.Windows.Controls.TreeView.

Example

The xref:System.Windows.Controls.TreeView.SelectedValuePath%2A property provides a way to specify a xref:System.Windows.Controls.TreeView.SelectedValue%2A for the xref:System.Windows.Controls.TreeView.SelectedItem%2A in a xref:System.Windows.Controls.TreeView. The xref:System.Windows.Controls.TreeView.SelectedItem%2A represents an object in the xref:System.Windows.Controls.ItemsControl.Items%2A collection and the xref:System.Windows.Controls.TreeView displays the value of a single property of the selected item. The xref:System.Windows.Controls.TreeView.SelectedValuePath%2A property specifies the path to the property that is used to determine the value of the xref:System.Windows.Controls.TreeView.SelectedValue%2A property. The examples in this topic illustrate this concept.

The following example shows an xref:System.Windows.Data.XmlDataProvider that contains employee information.

[!code-xamlTreeViewSelectedValue#XMLDataProvider]

The following example defines a xref:System.Windows.HierarchicalDataTemplate that displays the EmployeeName and EmployeeWorkDay of the Employee. Note that the xref:System.Windows.HierarchicalDataTemplate does not specify the EmployeeNumber as part of the template.

[!code-xamlTreeViewSelectedValue#HierarchicalDataTemplate]

The following example shows a xref:System.Windows.Controls.TreeView that uses the previously defined xref:System.Windows.HierarchicalDataTemplate and that sets the xref:System.Windows.Controls.TreeView.SelectedValue%2A property to the EmployeeNumber. When you select an EmployeeName in the xref:System.Windows.Controls.TreeView, the xref:System.Windows.Controls.TreeView.SelectedItem%2A property returns the EmployeeInfo data item that corresponds to the selected EmployeeName. However, because the xref:System.Windows.Controls.TreeView.SelectedValuePath%2A of this xref:System.Windows.Controls.TreeView is set to EmployeeNumber, the xref:System.Windows.Controls.TreeView.SelectedValue%2A is set to the EmployeeNumber.

[!code-xamlTreeViewSelectedValue#SelectedValuePath]

See also