Udostępnij za pośrednictwem


TreeView.SelectedValue Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Property SelectedValue As Object
public Object SelectedValue { get; private set; }

Property Value

Type: System.Object
The value of the SelectedItem property that is specified by the SelectedValuePath property, or nulla null reference (Nothing in Visual Basic) if no item is selected. The default value is nulla null reference (Nothing in Visual Basic).

Remarks

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.

Examples

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()
        InitializeComponent()

        ' 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", _
         Guid.NewGuid()))
        Topics.Add(DataGridTopic)
        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
        Get
            Return titleValue
        End Get
        Set(ByVal value As String)
            titleValue = value
        End Set
    End Property
    Private idValue As Guid
    Public Property Id() As Guid
        Get
            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)
        Get
            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)
        Me.New()
        Title = title1
        Id = id1
    End Sub
End Class
public partial class MainPage : UserControl
{
    // Create the topics collection.
    static public ObservableCollection<Topic> Topics = 
        new ObservableCollection<Topic>();
    public MainPage()
    {
        InitializeComponent();

        // 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()));
        Topic DataGridTopic = 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", 
                Guid.NewGuid()));
        Topics.Add(DataGridTopic);
        myTreeView.DataContext = Topics;

        //Associated a handler with the item changed event.
        myTreeView.SelectedItemChanged += 
            new RoutedPropertyChangedEventHandler<object>(myTreeView_SelectedItemChanged);
    }

    void myTreeView_SelectedItemChanged(object sender, 
        RoutedPropertyChangedEventArgs<object> e)
    {
        // Set the data context of the text block to the selected value.
        TreeView myTreeView = sender as TreeView;
        idTextBlock.DataContext = myTreeView.SelectedValue;
    }
}

// Simple business object.
public class Topic
{
    public string Title { get; set; }
    public Guid Id { get; set; }
    public ObservableCollection<Topic> ChildTopics { get; set; }
    private Topic()
    {
        ChildTopics = new ObservableCollection<Topic>();
    }
    public Topic(string title, Guid idValue) : this()
    {
        Title = title;
        Id = idValue;
    }
}
    <StackPanel x:Name="LayoutRoot" Background="White">
        <StackPanel.Resources>
            <sdk:HierarchicalDataTemplate x:Key="ChildTemplate" >
                <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" />
            </sdk:HierarchicalDataTemplate>
            <sdk:HierarchicalDataTemplate x:Key="NameTemplate" 
                ItemsSource="{Binding Path=ChildTopics}" 
                ItemTemplate="{StaticResource ChildTemplate}">
                <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" />
            </sdk:HierarchicalDataTemplate>
        </StackPanel.Resources>
        <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" />
        </StackPanel>
            </StackPanel> 

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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