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)
Property Value
Type: System.ObjectThe value of the SelectedItem property that is specified by the SelectedValuePath property, or null if no item is selected. The default value is null.
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.
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>
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.