Permalink
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (63 sloc) 4.74 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
ContextMenu Overview
03/30/2017
.net-framework
dotnet-wpf
article
controls, ContextMenu
ContextMenu controls [WPF], about ContextMenu controls
16909c42-799a-4561-91e0-7d69dcfeea91
25
dotnet-bot
dotnetcontent
wpickett

ContextMenu Overview

The xref:System.Windows.Controls.ContextMenu class represents the element that exposes functionality by using a context-specific xref:System.Windows.Controls.Menu. Typically, a user exposes the xref:System.Windows.Controls.ContextMenu in the [!INCLUDETLA#tla_ui] by right-clicking the mouse button. This topic introduces the xref:System.Windows.Controls.ContextMenu element and provides examples of how to use it in [!INCLUDETLA#tla_xaml] and code.

ContextMenu Control

A xref:System.Windows.Controls.ContextMenu is attached to a specific control. The xref:System.Windows.Controls.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 xref:System.Windows.Controls.Button. Users right-click the control to make the menu appear. Typically, clicking a xref:System.Windows.Controls.MenuItem opens a submenu or causes an application to carry out a command.

Creating ContextMenus

The following examples show how to create a xref:System.Windows.Controls.ContextMenu with submenus. The xref:System.Windows.Controls.ContextMenu controls are attached to button controls.

[!code-xamlContextMenu#1]

[!code-csharpContextMenu#2] [!code-vbContextMenu#2]

Applying Styles to a ContextMenu

By using a control xref:System.Windows.Style, you can dramatically change the appearance and behavior of a xref:System.Windows.Controls.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 xref:System.Windows.Controls.ContextMenu. The following examples show several ways to add styles to xref:System.Windows.Controls.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 xref:System.Windows.SystemColors.MenuHighlightBrushKey%2A as the xref:System.Windows.Controls.Control.Background%2A color and xref:System.Windows.SystemColors.MenuTextBrushKey%2A as the xref:System.Windows.Controls.Control.Foreground%2A color of the xref:System.Windows.Controls.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}}"/>  
</Style>  

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

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

See Also

xref:System.Windows.Controls.ContextMenu
xref:System.Windows.Style
xref:System.Windows.Controls.Menu
xref:System.Windows.Controls.MenuItem
ContextMenu
ContextMenu Styles and Templates
WPF Controls Gallery Sample