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 6613029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:10:09+00:00 2026-05-25T20:10:09+00:00

I have menu ServerList, I am adding the menuItems dynamically using C# code. It

  • 0

I have menu ServerList, I am adding the menuItems dynamically using C# code. It reads the servers list from file and populate the menu items. I have added the right click options for each server. Edit & Delete.

All this is working fine. the problem is how do I read actual server name when Edit/Detele is clicked.

Here is the code

       public MainWindow()
    {
        InitializeComponent();
        LoadMenuItems();
    }

    //Currently static values, but reads from file. later
    private void LoadMenuItems()
    {
        MenuItem item2 = new MenuItem();
        item2.Header = "Server1";
        AddContextMenu(item2);

        MenuItem item3 = new MenuItem();
        item3.Header = "Server2";
        AddContextMenu(item3);

        ActualMenu.Items.Add(item2);
        ActualMenu.Items.Add(item3);
    }

    private void AddContextMenu(MenuItem item)
    {
        MenuItem item1 = new MenuItem();
        item1.Header = "Edit";            
        item1.Click += item_Click;

        MenuItem item2 = new MenuItem();
        item2.Header = "Detlete";
        item2.Click += item_Click;

       ContextMenu menu = new ContextMenu();
        menu.Items.Add(item1);
        menu.Items.Add(item2);

        item.ContextMenu = menu;
    }

    void item_Click(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        string header = item.Header.ToString();            
    }      
  • 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-25T20:10:10+00:00Added an answer on May 25, 2026 at 8:10 pm

    For this use PlacementTarget.

     private void AddContextMenu(MenuItem item)
     {
        MenuItem item1 = new MenuItem();
        ....
        ContextMenu menu = new ContextMenu();
        ....
        menu.PlacementTarget = item;   /// 'Connects' context menu to source menu item.
        item.ContextMenu = menu;
     } 
    
     void item_Click(object sender, RoutedEventArgs e)
     {
        MenuItem item = sender as MenuItem;
        string header
           = ((MenuItem)((ContextMenu)((MenuItem)sender).Parent).PlacementTarget).Header;
     }  
    

    Cheers.

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

Sidebar

Related Questions

I have problems adding panorama items through code. Here is test xaml that works.
I have a menu where I am setting the selected value dynamically. Will I
I have menu designed using jQuery in oracle apex.The menu is like this Parent1
I have menu build on Richfaces using rich:menuitem. I need to determine, which menuitem
I have Menu in my app. I'm visualizing it using hierarchical data template: <MenuItem
On the .master file i have... <div id=menu> <ul> <li class=1><a href=#>One</a></li> <li><a href=#>Two</a></li>
I have a menu implemented using a set of nested accordions, 1 and 2
I have menu bar constructed from li and a inside. In css I'm trying
Hello i have created menu using ULs, LIs and CSS styles. On my page
I have a menu running off of a sitemap which one of the SiteMapNode

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.