【问题标题】:Is it possible to add Menu Items to a context menu during implementation?是否可以在实施过程中将菜单项添加到上下文菜单中?
【发布时间】:2013-08-06 12:36:21
【问题描述】:

我希望我问的是正确的问题,但这是我的情况。我有一个正在实施的TreeViewItem。在其中我设置/添加各种属性,其中之一是ContextMenu。我要做的就是将MenuItems 添加到ContextMenu 而不传递给函数等。

以下是我使用ContextMenu 实现TreeViewItem 的方法:

public static TreeViewItem Item = new TreeViewItem() //Child Node
{
        ContextMenu = new ContextMenu //CONTEXT MENU
        {
            Background = Brushes.White,
            BorderBrush = Brushes.Black,
            BorderThickness = new Thickness(1),

            //**I would like to add my MENUITEMS here if possible
        }
};

非常感谢!

【问题讨论】:

    标签: c# wpf contextmenu menuitem


    【解决方案1】:

    为此,WPF 我这样做了:

    TreeViewItem GreetingItem = new TreeViewItem()
        {
            Header = "Greetings",
            ContextMenu = new ContextMenu //CONTEXT MENU
            {
                Background = Brushes.White,
                BorderBrush = Brushes.Black,
                BorderThickness = new Thickness(1),
            }
        };
    
    // Create ContextMenu
    contextMenu = new ContextMenu();
    contextMenu.Closing += contextMenu_Closing;
    
    // Exit item
    MenuItem menuItemExit = new MenuItem
    {
          Header = Cultures.Resources.Exit,
          Icon= Cultures.Resources.close
    };
    menuItemExit.Click += (o, a) =>
    {
         Close();
    }
    
    // Restore item
    MenuItem menuItemRestore = new MenuItem
    {
        Header = Cultures.Resources.Restore,
        Icon= Cultures.Resources.restore1
    };
    menuItemRestore.Click += (o, a) =>
    {
         WindowState = WindowState.Normal;
    };
    
    contextMenu.Items.Add(menuItemRestore);
    contextMenu.Items.Add(menuItemExit);               
    
    GreetingItem.ContextMenu = contextMenu;
    

    您可以将其设置为任何支持此功能的元素。

    编辑:我是凭记忆写的,如果不准确,请见谅。但或多或少就是这个想法。

    【讨论】:

    • 这将帮助我为这些项目添加事件,非常感谢!
    【解决方案2】:

    Sonhja 的答案是正确的。为您的案例提供示例。

            TreeViewItem GreetingItem = new TreeViewItem()
            {
                Header = "Greetings",
                ContextMenu = new ContextMenu //CONTEXT MENU
                {
                    Background = Brushes.White,
                    BorderBrush = Brushes.Black,
                    BorderThickness = new Thickness(1),
                }
            };
    
            MenuItem sayGoodMorningMenu = new MenuItem() { Header = "Say Good Morning" };
            sayGoodMorningMenu.Click += (o, a) =>
            {
                MessageBox.Show("Good Morning");
            };
            MenuItem sayHelloMenu = new MenuItem() { Header = "Say Hello" };
            sayHelloMenu.Click += (o, a) =>
                {
                    MessageBox.Show("Hello");
                };
            GreetingItem.ContextMenu.Items.Add(sayHelloMenu);
            GreetingItem.ContextMenu.Items.Add(sayGoodMorningMenu);
            this.treeView.Items.Add(GreetingItem);
    

    【讨论】:

    • 我接受了这个答案,因为它是我用来让我的程序正常工作的答案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-06-03
    • 2011-09-21
    • 2013-12-27
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多