how to make mouse right button(click) select treeviewitem? RRS feed

  • Question

  • 1、how to make mouse right button(click) select treeviewitem?

    private void treeview_MouseRightButtonUP(object sender, MouseButtonEventArgs e)
    //how to make mouse right button(click) select treeviewitem?

    how to find treeviewitme when click the context menuitem in treeview?


    private void myHandler(object sender, MouseButtonEventArgs args)
      TreeViewItem item = args.Source as TreeViewItem;

      if(item != null)
         // do whatever you want with item
    the above solution doesn't work 
    In the case when TreeView items are autogenerated via attaching some DataProvider
    Tuesday, November 20, 2007 1:02 PM

All replies

  • 1. How to select a TreeViewItem by right mouse click?

    You can hook up to TreeViewItem.RightMouseButtonDown event to add the selection logic, here is an example:



        <Style TargetType="{x:Type TreeViewItem}">

          <EventSetter Event="TreeViewItem.MouseRightButtonDown" Handler="TreeViewItem_MouseRightButtonDown"/>




    private void TreeViewItem_MouseRightButtonDown(Object sender, MouseButtonEventArgs e)


        TreeViewItem item = sender as TreeViewItem;

        if (item != null)


            item.IsSelected = true;

            e.Handled = true;



    2. How to find TreeViewItem when clicking the context menu item in TreeView?

    Well, what type of scenario do you want to enable? could you be more specific about this?



    • Proposed as answer by JacobNeroth Monday, May 10, 2010 4:01 AM
    Friday, November 23, 2007 5:13 AM