wpf 向以编程方式添加的菜单项添加点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15851489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:30:24  来源:igfitidea点击:

Adding an click event to a programatically added menu item

c#wpfmenumenuitem

提问by Boardy

I am working on a C# WPF project and I am storing some items in an SQLite database, when the program loads, it then retrieves the items from the database and adds the items to the menu. What I then need to do is to allow the user to click on one of the added menu items and something is done based on what was clicked. I can't find anything on how to do this, below is the code for how I am adding the menu item to the menu programatically.

我正在处理一个 C# WPF 项目,我将一些项目存储在 SQLite 数据库中,当程序加载时,它然后从数据库中检索项目并将这些项目添加到菜单中。然后我需要做的是允许用户单击添加的菜单项之一,然后根据单击的内容完成某些操作。我找不到有关如何执行此操作的任何信息,以下是我如何以编程方式将菜单项添加到菜单的代码。

StoredDBConnectionManager storedDbConnectionManager = new StoredDBConnectionManager(Properties.Settings.Default.app_dbPassword);
List<string> connections = storedDbConnectionManager.getStoredConnections();

foreach (string connection in connections)
{
      mnuFileDBConnections.Items.Add(connection);
}

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

回答by Steve

Here's an example:

下面是一个例子:

XAML:

XAML:

<Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="200" />

Code behind:

后面的代码:

public MainWindow() {
    InitializeComponent();

    MenuItem item = new MenuItem { Header = "test" };
    item.Click += new RoutedEventHandler(item_Click);
    menu1.Items.Add(item);
}

public void item_Click(Object sender, RoutedEventArgs e) {
    MessageBox.Show("Hello!");
}

回答by Clint

There should be a MenuItemcontrol you can instantiate and use the connectionas its Headeror Content.

应该有一个MenuItem控件可以实例化并将connection用作它的Headeror Content

MenuItemwill then have a Clickevent handler against it or you can set the command.

MenuItem然后将有一个Click针对它的事件处理程序,或者您可以设置命令。

Ideally however, you should be retrieving the connectionscollection, setting it to a property on your modeland then have the menubound to that collection, that way it's a simple matter of making use of an ItemTemplatefor the menu.

然而,理想情况下,您应该检索connections集合,将其设置为您的属性model,然后menu绑定到该集合,这样使用ItemTemplate菜单就很简单了。

e.g.

例如

        StoredDBConnectionManager storedDbConnectionManager = new StoredDBConnectionManager(Properties.Settings.Default.app_dbPassword);
        List<string> connections = storedDbConnectionManager.getStoredConnections();

        foreach (string connection in connections)
        {
            var mi = new MenuItem()
            {
                Header = connection,
            };

            mi.Click += ConnectionMenuItemClicked;

            mnuFileDBConnections.Items.Add(mi);
        }

OR with binding:

或绑定:

    <Menu ItemsSource="{Binding Connections}">
        <Menu.ItemTemplate>
            <DataTemplate>
                <MenuItem Header="{Binding}" Click="ConnectionsMenuItem_Clicked">

                </MenuItem>
            </DataTemplate>
        </Menu.ItemTemplate>
    </Menu>

回答by David

foreach(string menuCaption from ...)
{
    MenuItem mi=new MenuItem();
    mi.Header = meniCaption;
    mi.Click += (s,e) =>
        {
             ...
        }
}