TreeView.SelectedValue Property

Gets the value of the property that is the specified by SelectedValuePath for the SelectedItem. This is a dependency property.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

[BindableAttribute(true)]
public Object SelectedValue { get; }
You cannot set this property in XAML.

Property Value

Type: System.Object
The value of the property that is specified by the SelectedValuePath for the SelectedItem, or null if no item is selected. The default value is null.

Identifier field

SelectedValueProperty

Metadata properties set to true

None

The SelectedValuePath property specifies the path to the property that is used to determine the value of the SelectedValue property. For example, assume that a TreeView is bound to a collection of objects of type Employee, which has two properties called EmployeeName and EnployeeNumber. You can use a HierarchicalDataTemplate to display the value of EmployeeName in the TreeView and set SelectedValuePath to "EmployeeNumber" to have SelectedValue return the value of EmployeeNumber.

The SelectedValue property is a read-only property. To change the value of a selected item in a TreeView, use the SelectedItem property to access the TreeViewItem.

The following example shows a TreeView that uses a HierarchicalDataTemplate to display the EmployeeName and EmployeeWorkDay properties of an Employee and sets the SelectedValue property to the EmployeeNumber. When you select an EmployeeName in the TreeView, the SelectedValue is set to the EmployeeNumber. For the complete sample, see TreeView That Specifies SelectedValuePath Sample.

<TreeView ItemsSource="{Binding Source={StaticResource myEmployeeData}, 
    XPath=EmployeeInfo}" 
    Name="myTreeView" 
    SelectedValuePath="EmployeeNumber" 
    />

<TextBlock Margin="10">SelectedValuePath: </TextBlock>
<TextBlock Margin="10,0,0,0" 
           Text="{Binding ElementName=myTreeView, 
                          Path=SelectedValuePath}"
           Foreground="Blue"/>

<TextBlock Margin="10">SelectedValue: </TextBlock>
<TextBlock Margin="10,0,0,0" 
           Text="{Binding ElementName=myTreeView, 
                          Path=SelectedValue}"
           Foreground="Blue"/>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft