Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/contextmenu-overview.md
T
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

4.4 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
ContextMenu Overview 03/30/2017
csharp
vb
controls [WPF], ContextMenu
ContextMenu controls [WPF], about ContextMenu controls
16909c42-799a-4561-91e0-7d69dcfeea91

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 user interface (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 Extensible Application Markup Language (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