右键单击菜单项并显示选项

发布于 2024-12-06 17:09:00 字数 1264 浏览 1 评论 0原文

我有菜单 ServerList,我使用 C# 代码动态添加菜单项。它从文件中读取服务器列表并填充菜单项。我为每个服务器添加了右键单击选项。编辑&删除。

这一切都运行良好。问题是单击“编辑/删除”时如何读取实际服务器名称。

这是代码

       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();            
    }      

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();            
    }      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脱离于你 2024-12-13 17:09:00

为此,请使用 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;
 }  

干杯。

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.

メ斷腸人バ 2024-12-13 17:09:00

默认情况下,MenuItem 的 Header 使用 TextBlock 来显示内容。因此,在这种情况下,您需要将 Header 转换为 TextBox,然后查看 Text 属性。

例如,

void item_Click(object sender, RoutedEventArgs e){
    string servername =  ((sender as MenuItem).Header as TextBlock).Text;
}

By default, the Header of a MenuItem uses a TextBlock to display content. So, in this case you need to convert the Header to a TextBox, then look at the Text property.

For example,

void item_Click(object sender, RoutedEventArgs e){
    string servername =  ((sender as MenuItem).Header as TextBlock).Text;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文