Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing library for ContextMenu

Tags:

c#

wpf

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?

like image 333
Bender Avatar asked Feb 13 '26 06:02

Bender


1 Answers

.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.

like image 116
Aaron Thomas Avatar answered Feb 15 '26 20:02

Aaron Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!