TreeView.SelectedValue Property

Gets the value of the SelectedItem property that is specified by the SelectedValuePath property.

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

Public Property SelectedValue As Object

Property Value

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

Dependency property identifier field: SelectedValueProperty

SelectedValuePath is used to specify which property value on the selected item is returned for SelectedValue. For example, if you have a TreeView bound to a collection of objects of type Employee, which has two properties named EmployeeName and EnployeeNumber. You can set SelectedValuePath to "EmployeeNumber" to have SelectedValue return the value of EmployeeNumber.

SelectedValue 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 code example shows how to use the SelectedValuePath, SelectedValue, and SelectedItemChanged event together to update a text block that is data bound to the selected value.

Partial Public Class MainPage
    Inherits UserControl
    ' Create the topics collection. 
    Public Shared Topics As New ObservableCollection(Of Topic)()
    Public Sub New()

        ' Add some topics to the collection. 
        Topics.Add(New Topic("Using Controls and Dialog Boxes", Guid.NewGuid()))
        Topics.Add(New Topic("Getting Started with Controls", Guid.NewGuid()))
        Dim DataGridTopic As New Topic("DataGrid", Guid.NewGuid())
    DataGridTopic.ChildTopics.Add(New Topic( _
            "Default Keyboard and Mouse Behavior in the DataGrid Control", Guid.NewGuid()))
    DataGridTopic.ChildTopics.Add(New Topic( _
        "How to: Add a DataGrid Control to a Page", Guid.NewGuid()))
    DataGridTopic.ChildTopics.Add(New Topic( _
        "How to: Display and Configure Row Details in the DataGrid Control", _
        myTreeView.DataContext = Topics

End Sub

Private Sub myTreeView_SelectedItemChanged(ByVal sender As Object, _
    ByVal e As RoutedPropertyChangedEventArgs(Of Object)) _
    Handles myTreeView.SelectedItemChanged
    ' Set the data context of the text block to the selected value. 
    Dim myTreeView As TreeView = TryCast(sender, TreeView)
    idTextBlock.DataContext = myTreeView.SelectedValue
End Sub
End Class

' Simple business object. 
Public Class Topic
    Private titleValue As String
    Public Property Title() As String
            Return titleValue
        End Get
        Set(ByVal value As String)
            titleValue = value
        End Set
    End Property
    Private idValue As Guid
    Public Property Id() As Guid
            Return idValue
        End Get
        Set(ByVal value As Guid)
            idValue = value
        End Set
    End Property
    Private childTopicsValue As ObservableCollection(Of Topic)
    Public Property ChildTopics() As ObservableCollection(Of Topic)
            Return childTopicsValue
        End Get
        Set(ByVal value As ObservableCollection(Of Topic))
            childTopicsValue = value
        End Set
    End Property
    Private Sub New()
        ChildTopics = New ObservableCollection(Of Topic)()
    End Sub
    Public Sub New(ByVal title1 As String, ByVal id1 As Guid)
        Title = title1
        Id = id1
    End Sub
End Class

    <StackPanel x:Name="LayoutRoot" Background="White">
            <sdk:HierarchicalDataTemplate x:Key="ChildTemplate" >
                <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" />
            <sdk:HierarchicalDataTemplate x:Key="NameTemplate" 
                ItemsSource="{Binding Path=ChildTopics}" 
                ItemTemplate="{StaticResource ChildTemplate}">
                <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" />
        <sdk:TreeView Width="400"  Height="200" ItemsSource="{Binding}" 
            ItemTemplate="{StaticResource NameTemplate}" x:Name="myTreeView" 
	    SelectedValuePath="Id" />
        <StackPanel Orientation="Horizontal" >
            <TextBlock Margin="5" Text="Selected topic GUID:" />
            <TextBlock FontWeight="Bold" Margin="5" Text="{Binding}" x:Name="idTextBlock" />


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions