ContextMenu Overview
Collapse the table of content
Expand the table of content

ContextMenu Overview

The ContextMenu class represents the element that exposes functionality by using a context-specific Menu. Typically, a user exposes the ContextMenu in the user interface (UI) by right-clicking the mouse button. This topic introduces the ContextMenu element and provides examples of how to use it in Extensible Application Markup Language (XAML) and code.

This topic contains the following sections.

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear. Typically, clicking a MenuItem opens a submenu or causes an application to carry out a command.

The following examples show how to create a ContextMenu with submenus. The ContextMenu controls are attached to button controls.

<Button Name="cmButton" Height="30">
  Button with Context Menu
    <ContextMenu Name="cm" Opened="OnOpened" Closed="OnClosed" StaysOpen="true">
      <MenuItem Header="File"/>
      <MenuItem Header="Save"/>
      <MenuItem Header="SaveAs"/>
      <MenuItem Header="Recent Files">
        <MenuItem Header="ReadMe.txt"/>
        <MenuItem Header="Schedule.xls"/>

btn = new Button();
btn.Content = "Created with C#";
contextmenu = new ContextMenu();
btn.ContextMenu = contextmenu;
mi = new MenuItem();
mi.Header = "File";
mia = new MenuItem();
mia.Header = "New";
mib = new MenuItem();
mib.Header = "Open";
mib1 = new MenuItem();
mib1.Header = "Recently Opened";
mib1a = new MenuItem();
mib1a.Header = "Text.xaml";

By using a control Style, you can dramatically change the appearance and behavior of a ContextMenu without writing a custom control. In addition to setting visual properties, you can also apply styles to parts of a control. For example, you can change the behavior of parts of the control by using properties, or you can add parts to, or change the layout of, a ContextMenu. The following examples show several ways to add styles to ContextMenu controls.

The first example defines a style called SimpleSysResources, which shows how to use the current system settings in your style. The example assigns MenuHighlightBrushKey as the Background color and MenuTextBrushKey as the Foreground color of the ContextMenu.

<Style x:Key="SimpleSysResources" TargetType="{x:Type MenuItem}">
  <Setter Property = "Background" Value= 
    "{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"/>
  <Setter Property = "Foreground" Value= 
    "{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>

The following example uses the Trigger element to change the appearance of a Menu in response to events that are raised on the ContextMenu. When a user moves the mouse over the menu, the appearance of the ContextMenu items changes.

<Style x:Key="Triggers" TargetType="{x:Type MenuItem}">
    <Trigger Property="MenuItem.IsMouseOver" Value="true">
      <Setter Property = "FontSize" Value="16"/>
      <Setter Property = "FontStyle" Value="Italic"/>
      <Setter Property = "Foreground" Value="Red"/>
© 2016 Microsoft