Enable left mouse click on wpf button control to show context menu.

    General discussion

  • The ContextMenu allows a control to expose functionality through the use of a Menu that is specific to the context of the control.
    If you add context menu to a button control in WPF application you can open the context menu on right click of the mouse. 
    I wanted to open this context menu on left click of the mouse. May be you too are looking for the same.
    Here is how you can do so.

    Lets begin with a button conrtol with context menu.

    <Button x:Uid="buttonAgentStatusButton"     
            ToolTip="Agent Status"    
        <Image Stretch="Uniform" Height="20" Width="20"      
          Source="..\Dictionaries\Midnight\Styles\Images\Ready.png" />                        
          <ContextMenu x:Uid="agentStatusContextMenu" x:Name="agentStatusContextMenu">             
             <MenuItem x:Uid="menuItemReadyStatus"     
                       ToolTip="Change Agent Status to Ready"    
                    <Image Height="Auto" Width="Auto"                                                    
             <MenuItem x:Uid="menuItemAuxiliaryStatus"     
                       ToolTip="Change Agent Status to Auxiliary"    
                    <Image Height="Auto" Width="Auto"     

    So now I have a button control with two items in the context menu. The right mouse click is available by default.

    Now lets add code to enable left mouse click.

    private void buttonAgentStatusButton_Click(object sender, RoutedEventArgs e)  
       agentStatusContextMenu.PlacementTarget = this;  
       agentStatusContextMenu.IsOpen = true;  

    And the work is done. Now you can open the context menu on either left mouse click or right mouse click.
    Friday, September 05, 2008 4:36 AM

All replies

  • This is exactly what I want/need to do.  I have one twist however you may be able to help me with, adding a MenuItem:

                            ItemSource="{Binding RelativeSource{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=ShortList}" 

    and to your hosting UserControl:
            public enum FullList  
            public ReadOnlyCollection<FullList> ShortList  
                    List<FullList> list = new List<FullList> 
                    return new ReadOnlyCollection<FullList>(list);  

    The problem is that if you right click, all works well, if you left click (on first time run), the List menu item is not populated.   I can't figure out why but it seems the binding is not updated until you right click.

    Any thoughts?

    Friday, September 26, 2008 6:11 PM
  • Ah... I missed setting the PlacementTarget.  Thanks for the post!

    Wednesday, October 15, 2008 9:21 PM
  • I'm having an absolutely awful time getting this to work. I followed your lead and tried PlacementTarget, but that didn't help. When I left-click my Image (note I'm doing this on MouseLeftButtonDown) and call contextMenu.IsOpen = true, nothing appears at all. Worse, the left mouse button is now dead unless I click elsewhere and do other things until it starts working again.

    Right-click works perfectly, and if I left-click while the menu is already open from a right-click it works fine too.

    Note I am populating the menu manually on ContextMenuOpening event, which isn't seeming to be called when I set IsOpen to true, but even if I populate the menu manually before setting that, still nothing appears!

    Update: Lame! I changed the event to PREVIEWMouseLeftButtonDown and now it works. Sheesh.
    • Edited by chaiguy1337 Tuesday, October 28, 2008 7:35 PM
    Tuesday, October 28, 2008 7:25 PM