Posted by: Zeeshan Amjad | October 25, 2009

Displaying Context Menu

Display context menu in WPF application is not very difficult task. Let’s take a look at simple example where we are displaying the context menu in the tab control. Context menu is defined by the context menu class that is inherited by MenuBase class.

Here is a partial class hierarchy of MenuBase class.


Here is a XAML code to display the context menu on Tab control.

  1: <Window x:Class="objDataProvider.Window1"
  2:     xmlns=""
  3:     xmlns:x=""
  4:     xmlns:System="clr-namespace:System;assembly=mscorlib"
  5:     xmlns:odp="clr-namespace:objDataProvider"
  6:     Title="Context Menu" Height="300" Width="400">    
  7:     <TabControl Margin="5">
  8:         <TabControl.ContextMenu>
  9:             <ContextMenu HasDropShadow="True">
 10:                 <MenuItem Header="Tab First Item"/>
 11:                 <MenuItem Header="Tab Second Item"/>
 12:                 <MenuItem Header="Tab Third Item"/>
 13:             </ContextMenu>
 14:         </TabControl.ContextMenu>
 15:         <TabItem Header="item 1">
 16:             <TabItem.ContextMenu>
 17:                 <ContextMenu HasDropShadow="True">
 18:                     <MenuItem Header="First Item"/>
 19:                     <MenuItem Header="Second Item"/>
 20:                 </ContextMenu>
 21:             </TabItem.ContextMenu>
 22:         </TabItem>
 23:         <TabItem Header="item 2">
 24:             <TabItem.ContextMenu>
 25:                 <ContextMenu HasDropShadow="True">
 26:                     <MenuItem Header="First Item"/>
 27:                     <MenuItem Header="Second Item"/>
 28:                 </ContextMenu>
 29:             </TabItem.ContextMenu>
 30:         </TabItem>
 31:     </TabControl>
 32: </Window>

Note it will display three menu items when click on the tab control, but only two menu items when we right click on the tab here. Here is the output of the program when user click on the tab control.


And here is the output of the program when user click on the tab header.



  1. […] Menu in List box item We have already seen an example of Context Menu here and here. This time we are going to implement the context menu with list box item. In addition we […]

  2. nice! easy

    • Thanks to like it.
      Zeeshan Amjad

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: