3

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.

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.

3 Answers 3

6

Here's an example:

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!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

My click is not being called.
1

There should be a MenuItem control you can instantiate and use the connection as its Header or Content.

MenuItem will then have a Click event handler against it or you can set the command.

Ideally however, you should be retrieving the connections collection, setting it to a property on your model and then have the menu bound to that collection, that way it's a simple matter of making use of an ItemTemplate for the menu.

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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.