views:

364

answers:

3

Im adding items to TreeView control via ItemsSource property and ItemTemplate property to set the template for TreeViewItem. How can i add an event handler to handle selection change event on TreeViewItems?
For now my ItemTemplate looks like this:

<Window.Resources><DataTemplate x:Key="PeerDetailTemplate">
        <TextBlock Text="{Binding DESCRIPTION}" Tag="{Binding ID}" GotFocus="GetModules"/>
</DataTemplate></Window.Resources>

But it doesnt work (GetModules is not called). Im new to WPF, so show me the right direction to do such things, please.

+1  A: 

You'll need to add an event handler to the TreeView's SelectedItemChanged event.

<TreeView x:Name="myTreeView"
          SelectedItemChanged="myTreeView_SelectedItemChanged"
          ItemTemplate="{StaticResource PeerDetailTemplate} />

Since this is fired after the selection is changed, you can use the TreeView's selected item property to access the tree view item:

private void myTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeViewItem selectedItem = (TreeViewItem)myTreeView.SelectedItem;
        // do stuff
    }
Robin
+1  A: 

If you want to capture the SelectedItemChanged event in a TreeView, then you need to set the event handler on the parent node, i.e.,

XAML

<StackPanel>
    <TreeView SelectedItemChanged="OnTreeViewSelectedItemChanged">          
        <TreeViewItem Header="Desktop">
            <TreeViewItem Header="Computer" />
            <TreeViewItem Header="My Documents" />
            <TreeViewItem Header="c:\" />
        </TreeViewItem>
        <TreeViewItem Header="Recyle Bin" >
            <TreeViewItem Header="foo.txt" />
            <TreeViewItem Header="bar.txt" />
            <TreeViewItem Header="fizz.buzz" />
        </TreeViewItem>
        <TreeViewItem Header="Control Panel" >
            <TreeViewItem Header="Programs" />
            <TreeViewItem Header="Security" />
            <TreeViewItem Header="User Accounts" />
        </TreeViewItem>
    </TreeView>

    <TextBlock Margin="20" x:Name="MyTextBlock" />
</StackPanel>

Code Behind:

private void OnTreeViewSelectedItemChanged( object sender, RoutedPropertyChangedEventArgs<object> e )
{
    MyTextBlock.Text = ( (TreeViewItem) ( (TreeView) sender ).SelectedItem ).Header.ToString();
}
Metro Smurf
+1  A: 

Selection and Selection and focus are two different concepts. It sounds like you're interested in selection, which in this case is a property of the TreeView. Event TreeView.SelectedItemChanged will notify you of selection changes and property TreeView.SelectedItem will tell you what is selected.

Curt Nichols
Yes, did like in previous answers said..It helped. Thank you all for the answers, but as answer ill mark the first one. The rest will be +1
nihi_l_ist
And does that mean that even TreeViewItem that is deep inside the TreeView structure will be shown in SelectedItem?
nihi_l_ist
yes, the SelectedItemChanged event will always capture the current SelectedItem (TreeViewItem) no matter how deeply nested the TreeViewItem is within the TreeView.
Metro Smurf
Thank you for answers!
nihi_l_ist