none
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"     
            x:Name="buttonAgentStatusButton"    
            Height="28"    
            Width="26"                                            
            Cursor="Hand"    
            ToolTip="Agent Status"    
            Focusable="True"    
            Visibility="Hidden"      
            HorizontalAlignment="Right"    
            BorderBrush="Transparent"     
            BorderThickness="0"     
            Background="#FFDCDCDC"    
            Margin="0,-2,0,10"    
            Click="buttonAgentStatusButton_Click">    
                                                            
        <Button.BitmapEffect>    
          <DropShadowBitmapEffect/>    
        </Button.BitmapEffect>    
        
        <Image Stretch="Uniform" Height="20" Width="20"      
          Source="..\Dictionaries\Midnight\Styles\Images\Ready.png" />                        
                            
        <Button.ContextMenu>    
          <ContextMenu x:Uid="agentStatusContextMenu" x:Name="agentStatusContextMenu">             
             <MenuItem x:Uid="menuItemReadyStatus"     
                       x:Name="menuItemReadyStatus"    
                       Header="Ready"     
                       IsEnabled="False"     
                       HorizontalAlignment="Left"     
                       Margin="10,0,0,0"     
                       Height="Auto"     
                       Width="Auto"     
                       ToolTip="Change Agent Status to Ready"    
                       Click="menuItemReadyStatus_Click">    
                           
                  <MenuItem.Icon>    
                    <Image Height="Auto" Width="Auto"                                                    
                      Source="..\Dictionaries\Midnight\Styles\Images\Ready.png"     
                      Margin="15,0,0,0"/>    
                  </MenuItem.Icon>    
             </MenuItem>    
               
             <MenuItem x:Uid="menuItemAuxiliaryStatus"     
                       x:Name="menuItemAuxiliaryStatus"     
                       Header="Auxiliary"     
                       IsEnabled="True"     
                       HorizontalAlignment="Left"     
                       Margin="10,0,0,0"     
                       Height="Auto"     
                       Width="Auto"      
                       ToolTip="Change Agent Status to Auxiliary"    
                       Click="menuItemAuxiliaryStatus_Click">    
                           
                 <MenuItem.Icon>    
                    <Image Height="Auto" Width="Auto"     
                      Source="..\Dictionaries\Midnight\Styles\Images\Auxiliary.png"     
                      Margin="15,0,0,0"/>    
                 </MenuItem.Icon>    
        
            </MenuItem>    
         </ContextMenu>    
       </Button.ContextMenu>    
    </Button>   

    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:

                        <MenuItem   
                            Header="List"   
                            ItemSource="{Binding RelativeSource{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=ShortList}" 
                            /> 
     

    and to your hosting UserControl:
            public enum FullList  
            {  
                One,  
                Two,  
                Three,  
                Four,  
                Five  
            }  
     
            public ReadOnlyCollection<FullList> ShortList  
            {  
                get  
                {  
                    List<FullList> list = new List<FullList> 
                    {  
                        FullList.One,  
                        FullList.Two,  
                        FullList.Three  
                    };  
                    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