* 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
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 |
|
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]