Denis
Denis

Reputation:

Show menu programmatically in WPF

How can I open menu (System.Windows.Controls.Menu) programmatically in WPF?

Upvotes: 23

Views: 35874

Answers (5)

Danil
Danil

Reputation: 895

        private void MainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            IncList.ItemsSource = m_DataSource;
            IncList.ContextMenu = new ContextMenu();
            IncList.ContextMenu.Items.Add(new MenuItem() { Header = "Test1" });
            IncList.ContextMenu.Items.Add(new MenuItem() { Header = "Test2" });
        }

Upvotes: 1

Joe Sonderegger
Joe Sonderegger

Reputation: 804

private void button_Click(object sender, RoutedEventArgs e)
{
    var button= sender as FrameworkElement;
    if (button != null)
    {
        button.ContextMenu.IsOpen = true;
    }
}

Upvotes: 4

pablosan
pablosan

Reputation: 41

void CmsBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    box = sender as WpfBox;
    ContextMenu cms = new ContextMenu();
    e.Handled = true;
    ...
}

Upvotes: 0

Dmitry
Dmitry

Reputation: 327

Check out this example on how to open a context menu.

http://www.uxpassion.com/2009/01/how-to-enable-and-show-context-menu-on-left-click-in-wpf/

In summary

You can just call:

YourContextMenu.IsOpen = true;

This will display the context menu, just make sure its associated with a FrameworkElement on which it is displaying)

Upvotes: 28

Andreas Grech
Andreas Grech

Reputation: 107950

Get hold of the menu item, and do this :

_menuItem.IsSubmenuOpen = true;

Upvotes: 29

Related Questions