The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TreeView.SelectedItemChanged Event

Occurs when the value of the SelectedItem property changes.

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

public event RoutedPropertyChangedEventHandler<Object> SelectedItemChanged
<sdk:TreeView SelectedItemChanged="eventHandler"/>

For more information about handling events, see Events Overview for Silverlight.

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

        // 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());
            new Topic("Default Keyboard and Mouse Behavior in the DataGrid Control", 
            new Topic("How to: Add a DataGrid Control to a Page", 
            new Topic("How to: Display and Configure Row Details in the DataGrid Control", 
        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">
            <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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft