Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:42:52+00:00 2026-05-16T00:42:52+00:00

I have a WPF treeview with dynamic levels (TreeView Item uses a Hierachical data

  • 0

I have a WPF treeview with dynamic levels (TreeView Item uses a Hierachical data template which has a bindable richtextbox) for my application I need to handle TreeViewItem’s PreviewMouseRightButtonDown event.

When I right click on a treenode based on the node’s level this event gets hit multiple times (equal to the treeview item’s level. I think this is because of the tunnelling nature of this preview events)

Can someone please help me to identify the correct hit which gives the exact treeview item I have right clicked on?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:42:53+00:00Added an answer on May 16, 2026 at 12:42 am

    The easiest approach is to use the bubbling event MouseRightButtonDown instead of the tunneling event PreviewMouseRightButtonDown. You can mark the routed event as handled by setting the Handled property of the EventArgs to true, which will stop further event handlers from being called. That way, only the deepest TreeViewItem will receive the event.

    If you can’t use the Preview event, the other approach is to use the OriginalSource property from the EventArgs to find the UI element that was actually clicked. This will probably be your RichTextBox, so you will need to use a method to find a visual ancestor of type TreeViewItem. There is an example of a method to get an ancestor of a given type at http://www.wpftutorial.net/LogicalAndVisualTree.html:

    public static class VisualTreeHelperExtensions
    {
        public static T FindAncestor<T>(DependencyObject dependencyObject)
            where T : class
        {
            DependencyObject target = dependencyObject;
            do
            {
                target = VisualTreeHelper.GetParent(target);
            }
            while (target != null && !(target is T));
            return target as T;
        }
    }
    

    So, you could call ((DependencyObject)e.OriginalSource).FindAncestor<TreeViewItem>() to find the TreeViewItem that was clicked. If you do it this way, you should attach the event handler to the TreeView itself rather than the TreeViewItems. This will catch a click in any TreeViewItem, since they are all within the tree, but it will only be called a single time.


    Edit: As you note, that method doesn’t work if the target is a FrameworkContentElement because it is not a Visual. You can do something like this instead:

    public static class VisualTreeHelperExtensions
    {
        public static T FindAncestor<T>(object dependencyObject)
            where T : DependencyObject
        {
            var target = (DependencyObject)dependencyObject;
            do
            {
                var visualParent = target is Visual ? VisualTreeHelper.GetParent(target) : null;
                if (visualParent != null)
                {
                    target = visualParent;
                }
                else
                {
                    var logicalParent = LogicalTreeHelper.GetParent(target);
                    if (logicalParent != null)
                    {
                        target = logicalParent;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            while (!(target is T));
            return (T)target;
        }
    }
    

    Then you should be able to get the TreeViewItem from the OriginalSource by doing VisualTreeHelperExtensions.FindAncestor<TreeViewItem>(e.OriginalSource).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My .NET 4 application uses the WPF TreeView which I have enhanced to enable
I have WPF treeView which has ControlTemplate which show every node of treeView consisting
I have a WPF TreeView with 2 levels of data and 2 HierarchicalDataTemplate to
I have a TreeView WPF with 2 Levels of Data. I have deleted the
I have a little WPF app, that has a TreeView to display hierarchical data.
In my WPF application, I have a treeview. This treeview is bound to a
I have WPF Form which has many buttons with the same code. Appearance of
I have a WPF TreeView which triggers a SelectedItemChanged event. Whenever I select an
I have a wpf c# application, that loads tasks to a treeView from a
Hey guys, I have a WPF TreeView that has three nodes, I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.