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

  • Home
  • SEARCH
  • 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 3344554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:04:58+00:00 2026-05-18T01:04:58+00:00

Silverlight 4 now include the option for creating a context menu upon right clicking.

  • 0

Silverlight 4 now include the option for creating a context menu upon right clicking. Can anyone provide me with an example of a treeview with a right click context menu for the treeview?

Ultimately I want a the menu to show different options depending upon the node depth selected – bonus points if the example includes this!

  • 1 1 Answer
  • 0 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-18T01:04:59+00:00Added an answer on May 18, 2026 at 1:04 am

    You can use this open source menu for this:

    http://sl4popupmenu.codeplex.com

    The control supports right click on TreeViews out of the box. The code has been adapted from the sample code on the homepage to use a TreeView instead of a DataGrid:

        private void GenerateMenu()
        {
            var data = new ObservableCollection<string>("Item 1,Item 2,Item 3,Item 4,Item 6,Item 7,Item 8".Split(','));
            TreeView treeView1 = new TreeView() { Margin = new Thickness(50), ItemsSource = data };
            this.LayoutRoot.Children.Add(dataGrid1);
    
            // Create the submenu
            var pmTimeSub = new PopupMenu();
            pmTimeSub.AddItem("Time Now", null);
            // Create the main menu
            var pm = new PopupMenu();
            pm.AddItem("Delete row", delegate { data.RemoveAt(dataGrid1.SelectedIndex); });
            pm.AddSeparator();
            pm.AddSubMenu(pmTimeSub, "Get Time ", "images/arrow.png", null, null, false, null); 
            // Attach the submenu pmTimeSub
            pm.AddSeparator();
            pm.AddItem("Demo2", delegate { this.Content = new Demo2(); });
    
            // Set dataGrid1 as the trigger element
            pm.AddTrigger(TriggerTypes.RightClick, treeView1);
    
            // Showing main menu
            pm.Showing += (sender, e) =>
            {
                pm.PopupMenuItem(0).Header = "Delete " + treeView1.SelectedItem;
                TreeViewItem tvi = pm.GetClickedElement<TreeViewItem>();
    
                // Add code to calculate the node depth here using the GetParentTreeViewItem method
                // Add code to modify the menu items according to the node depth value.
    
                pm.PopupMenuItem(0).IsVisible =
                pm.PopupMenuItem(1).IsVisible = tvi != null;
            };
            // Showing submenu
            pmTimeSub.Showing += delegate
            {
                pmTimeSub.PopupMenuItem(0).Header = DateTime.Now.ToLongTimeString();
            };
        }
    

    Note that the code does not allow you to show different menus upon the node depth yet. To do this you can use the following method to get the parent of the TreeViewItem that was clicked:

     private static TreeViewItem GetParentTreeViewItem(DependencyObject item) 
     { 
         if (item != null) 
         { 
             DependencyObject parent = VisualTreeHelper.GetParent(item); 
             TreeViewItem parentTreeViewItem = parent as TreeViewItem; 
             return parentTreeViewItem ?? GetParentTreeViewItem(parent); 
         }    
         return null; 
     } 
    

    From there you can determine depth of the node by calling the GetParentTreeViewItem function in a loop until the parent is null. You would place this code in the event where the menu is being shown and then add the necessary code in there to show the appropriate menu.

    Hope this helps.

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

Sidebar

Related Questions

Now that Silverlight 2 has finally shipped. I'm wondering if anyone has put together
Right now I have a silverlight app that gets data from a mvc site.
Just playing around with the now released Silverlight 2.0. I'm trying to put a
In Silverlight how can I launch / navigate to another page?
I have inherited some Silverlight\WCF code and now need to implement some new features.
I've developed a Silverlight client that has worked fine on regular HTTP, but now
Using Silverlight 3 to build a file upload app. It can upload files of
The MSDN documentation for Silverlight now states in several places: The entire Triggers syntax
Silverlight 4 now provides controls to handle Drag and Drop actions. All the Target
I have to build small (for now) admin app in Silverlight2, and 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.