Bender
Bender

Reputation: 523

Missing library for ContextMenu

I'm using a code for get the name of the control that fire the ContextMenu, but the compiler return this message on this line: ContextMenu menu = menuItem.GetContextMenu();

MenuItem does not contain a definition of GetContextMenu and found no extension method 'GetContextMenu' accepting a first argument of type 'MenuItem'. Probably missing a using directive or an assembly reference.

The same error here:

Control sourceControl = menu.SourceControl;

This is my method:

private void ClearTable_Click(object sender, RoutedEventArgs e)
    {
        // Try to cast the sender to a MenuItem
        MenuItem menuItem = sender as MenuItem;
        if (menuItem != null)
        {
            // Retrieve the ContextMenu that contains this MenuItem
            ContextMenu menu = menuItem.GetContextMenu();

            // Get the control that is displaying this context menu
            Control sourceControl = menu.SourceControl;
        }
    }

Which library I must to add?

Upvotes: 3

Views: 2107

Answers (1)

Aaron Thomas
Aaron Thomas

Reputation: 5281

.GetContextMenu() is a method of the Menu class in System.Windows.Forms namespace (in System.Windows.Forms.dll). Therefore it's not designed to work on WPF controls... although with some tweaking it may be possible.

If you're doing this in WPF, you'll want to use ContextMenu class in the System.Windows.Controls namespace, with its methods.

Upvotes: 1

Related Questions