Export (0) Print
Expand All

TreeView.SelectedValuePath Property

Gets or sets the property path that is used to get the SelectedValue property of the SelectedItem property in a TreeView.

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

public string SelectedValuePath { get; set; }
<sdk:TreeView SelectedValuePath="propertyPath"/>

XAML Values

propertyPath

The property path for the property whose value is returned as the selected value. See Property Path Syntax.

Property Value

Type: System.String
The property path that is used to get the SelectedValue property of the SelectedItem property in a TreeView. The default value is Empty.

Dependency property identifier field: SelectedValuePathProperty

SelectedValuePath is used to specify which property value on the selected item is returned for SelectedValue. For example, if you have TreeView bound to a collection of objects of type Employee, which has two properties named EmployeeName and EmployeeNumber. 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.

Run this sample


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> 


Silverlight

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

Show:
© 2014 Microsoft