6

I have a TreeView in wpf how can one get the TreeView node click event so that I can get the value of the node on which the user has clicked?

Xaml

<Grid Height="258" Width="275">
    <TreeView Height="258" HorizontalAlignment="Left" Name="treeView1" VerticalAlignment="Top" Width="275">    
    </TreeView>
</Grid>

I have populated this TreeView from the C# code. What event methode do I need to write into c# code to get the value of clicked node by the user in my c# code.

Code Behind to Load

TreeViewItem treeItem = null;
treeItem = new TreeViewItem();
treeItem.Header = "Name";
4
  • where is the c# code? you need clicked node or selected node? usually after click a node gets selected too. Commented Jul 22, 2014 at 7:02
  • @pushpraj Actually sir the C# code is long .So i have posted only small snippet that i have added into Window_loaded of the app Commented Jul 22, 2014 at 7:07
  • @Ok Sir.I need the Clicked One sir.. Commented Jul 22, 2014 at 7:08
  • @pushpraj Sir string header=item.Header; Is it Correct? Commented Jul 22, 2014 at 7:29

3 Answers 3

7

Since there is no Click event available for TreeViewItem or TreeView so here are possible workaround for the same

you have two options from C# code

using MouseLeftButtonUp which will trigger every time the mouse left button is released on the item, which is similar to click

    void preparemethod()
    {
        ...
        TreeViewItem treeItem = null;
        treeItem = new TreeViewItem();
        treeItem.Header = "Name";
        treeItem.MouseLeftButtonUp += treeItem_MouseLeftButtonUp;
    }

    void treeItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        //your logic here
    }

or use Selected as trigger, may not trigger if a selected element is clicked

    void preparemethod()
    {
        ...
        TreeViewItem treeItem = null;
        treeItem = new TreeViewItem();
        treeItem.Header = "Name";
        treeItem.Selected += treeItem_Selected;
    }

    void treeItem_Selected(object sender, RoutedEventArgs e)
    {
        //your logic here
    }

in both of the approach the sender is the node which has been clicked. you can use as

example

    void treeItem_Selected(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = sender as TreeViewItem;
        //you can access item properties eg item.Header etc. 
        //your logic here 
    }
Sign up to request clarification or add additional context in comments.

15 Comments

Ok Sir .As i am first time working on wpf So how to get the selected value from the sender into string
you can access item properties eg item.Header etc. after casting sender to TreeViewItem, see updated answer.
if I am not wrong the item in TreeViewItem item = sender as TreeViewItem; is the TreeViewItem which is been selected. if not then please clarify what do you mean by item?
I am bit lost here, item is the node which is clicked, item.Header is the text what you see in the node text and item.Items is it's child items. if the value is none of them then what else is the value which you are interested in? where do you set that value?
item.Items is a collection of objects the sub items so you can use as TreeViewItem child1 = item.Items[0] as TreeViewItem; or you can access in a foreach loop on item.Items. foreach(TreeViewItem child in item.Items) { //your logic here}
|
1

If you need to take a more MVVM friendly approach you can use an interaction trigger on your TreeView ItemTemplate and bind the command with a parameter of the treeview item object back to your parent ViewModel:

<TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubItems}">

                    <TextBlock Text="{Binding Header, Mode=OneWay}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <i:InvokeCommandAction Command="{Binding DataContext.SelectedTreeViewItemClickedCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"  CommandParameter="{Binding ElementName=trvMainMenu,Path=SelectedItem}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    </TextBlock>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

Comments

0

If one has added a context menu to a treeview, the onclick event only returns the menu item it was clicked on, its the visual tree.

A better way to get the actual treeview item is to do these steps

  1. Name the TreeView which the operation in question is occurring such as tView for this example.
  2. In the click event of the menu (or any other event actually) get the current selected item var node = tView.SelectedItem as TreeViewItem

If there is no currently selected item, tell the user to select the item and do the process again. Once selected, at that point you have the actual tree view item to work with and do what is needed.


<TreeView x:Name="tView"
            HorizontalAlignment="Stretch" >
    <TreeView.ContextMenu>
        <ContextMenu>
            <MenuItem Click="CopyChildrenToClipboard"
                      Header="Copy Children To Clipboard">
                <MenuItem.Icon>
                    <Image Source="assets/check_green.png"
                           Height="16" />
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </TreeView.ContextMenu>
</TreeView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.