Why does not the ContextMenu(popup menu) show up?

11,724

Came across this problem myself, I used this:

ContextMenu.IsOpen = true;

MSDN Documentation on ContextMenu

Share:
11,724
user1298925
Author by

user1298925

Updated on June 05, 2022

Comments

  • user1298925
    user1298925 almost 2 years

    The following class derives from System.Windows.Controls.UserControl. In said class I call OpenFileDialog to open a XAML file (workflow file). Next, I implement a dynamic menu when right clicking the mouse. The menu does not show up. Is this a threading problem or a UI problem? In my research I've been unable to discover a solution.

    Thanks in advance.

    private void File_Open_Click(object sender, RoutedEventArgs e)
    {
        var fileDialog = new OpenFileDialog();
    
        fileDialog.Title  = "Open Workflow";
        fileDialog.Filter = "Workflow| *.xaml";
    
        if (fileDialog.ShowDialog() == DialogResult.OK)
        {
            LoadWorkflow(fileDialog.FileName);
            MouseDown += new System.Windows.Input.MouseButtonEventHandler(mouseClickedResponse);
         }
    }
    
    private void mouseClickedResponse(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (e.RightButton == MouseButtonState.Pressed)
        {
             LoadMenuItems();
        }
    }
    
    private void LoadMenuItems()
    {
        System.Windows.Controls.ContextMenu contextmenu = new System.Windows.Controls.ContextMenu();   
        System.Windows.Controls.MenuItem item1 = new System.Windows.Controls.MenuItem();
        item1.Header = "A new Test";
        contextmenu.Items.Add(item1);
        this.ContextMenu = contextmenu;
        this.ContextMenu.Visibility = Visibility.Visible;
    }
    
  • user1298925
    user1298925 over 11 years
    The problem is that my contextMenu does not have a Show method? I don't know why.
  • user1298925
    user1298925 over 11 years
    thanks for your reply. But in my case this.ContextMenu does not have a Show method. I get red line under the "Show" and it does not compile. I am however using system.Windows.Controls in my code. Could it be I need to be using a different assembly or a diffferent version?
  • user1298925
    user1298925 over 11 years
    I also tried your solution it dd not work probably one reason would be that I can't use this line of your code :this.ContextMenu.Show(this, e.Location); I am open for suggestions, thanks again
  • flix
    flix over 11 years
    Sorry, I just recognized that you are using Windows Presentation Foundation, not Windows Forms. The WPF ContextMenu doesn't have a Show method. I'm not familiar with WPF, but this http://wpftutorial.net/ContextMenu.html could help you. And this ContextMenu is the correct documentation page for your class.