ContextMenu In WPF

ContextMenu is an item control which can hold the items, like string panels or images. Basic usage of ContextMenu is to provide different functions you have implemented related to the control.

As mentioned on MSDN page, FrameworkElement or FrameworkContentElement can have a ContextMenu and you cannot change the behavior of it, as it is automatically placed inside a popup control.

For example, take a look at the below article (where I have mentioned about exporting DataGrid to Excel, using C#).

In that article, I have used a button to export the data to Excel but here, I would use the ContextMenu which is the easiest way to do such a thing.

For example:

Take a look at the below DataGrid where I have listed the records from the database.

Our goal is to have ContextMenu (or right-click menu for the Grid) whereupon clicking the menu item, it should export the data to Excel.

XAML

  1. <DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="305,60,0,0" Grid.Row="2" VerticalAlignment="Top">  
  2.             <DataGrid.ContextMenu>  
  3.                 <ContextMenu BorderBrush="Black" BorderThickness="1">  
  4.                       
  5.                     <MenuItem Header="_Export as Excel"   
  6.                               Click="MenuItem_Click"  
  7.                               />  
  8.                     <MenuItem Header="_Menu Item1"  
  9.                               Click="MenuItem_Click_1"                                
  10.                               />  
  11.                 </ContextMenu>  
  12.             </DataGrid.ContextMenu>  
  13.               
  14.         </DataGrid>  
Menu Item handlers
  1. private void MenuItem_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             //convert datasource to datatable and pass it to the method that converts datatable to Excel  
  4.         }  
  5.   
  6.         private void MenuItem_Click_1(object sender, RoutedEventArgs e)  
  7.         {  
  8.             MessageBox.Show("Clicked on Menu Item 1...""Information", MessageBoxButton.OK, MessageBoxImage.Information);  
  9.         }  
Output

Output

Now, if you click on Export as Excel item, it would invoke the handler associated with it and likewise, for the other items.

Output

Output of clicking on the Menu item 1

Not only for the Grids, but here is another example (from MSDN), where you will find the ContextMenu useful while dealing with printing of the labels or texts.

XAML
  1. <TextBlock Background="#fffcea" x:Name="textBlock" HorizontalAlignment="Left" Margin="651,71,0,0" TextWrapping="Wrap" Text="This is a sample text data to test Context Menu.." VerticalAlignment="Top" Height="82" Width="256" >  
  2.             <TextBlock.ContextMenu>  
  3.                 <ContextMenu>  
  4.                 <MenuItem Header="_Bold"  
  5.                     IsCheckable="True"  
  6.                     Checked="Bold_Checked"  
  7.                     Unchecked="Bold_Unchecked" />  
  8.                 <MenuItem Header="_Italic"  
  9.                     IsCheckable="True"  
  10.                     Checked="Italic_Checked"  
  11.                     Unchecked="Italic_Unchecked" />  
  12.                 <Separator />  
  13.                 <MenuItem Header="I_ncrease Font Size"  
  14.                     Click="IncreaseFont_Click" />  
  15.                 <MenuItem Header="_Decrease Font Size"  
  16.                     Click="DecreaseFont_Click" />  
  17.                 </ContextMenu>  
  18.             </TextBlock.ContextMenu>  
  19.         </TextBlock>  
Code behind handling click events
  1. private void DecreaseFont_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     textBlock.FontSize -= 2;//make a check for 0 or –ve values  
  4. }  
  5.   
  6. private void Italic_Unchecked(object sender, RoutedEventArgs e)  
  7. {  
  8.     textBlock.FontStyle = FontStyles.Italic;  
  9. }  
  10.   
  11. private void Bold_Unchecked(object sender, RoutedEventArgs e)  
  12. {  
  13.     textBlock.FontWeight = FontWeights.Normal;  
  14. }  
  15.   
  16. private void Bold_Checked(object sender, RoutedEventArgs e)  
  17. {  
  18.     textBlock.FontWeight = FontWeights.Bold;  
  19. }  
  20.   
  21. private void Italic_Checked(object sender, RoutedEventArgs e)  
  22. {  
  23.     textBlock.FontStyle = FontStyles.Normal;  
  24.   
  25. }  
  26.   
  27. private void IncreaseFont_Click(object sender, RoutedEventArgs e)  
  28. {  
  29.     textBlock.FontSize += 2;  
  30. }  
Output

Output

Output

This is just a simple way of creating and executing the menu items. You can explore it based on your requirement.

Please leave your questions/concerns in the comments. 

Up Next
    Ebook Download
    View all
    Learn
    View all