Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ContextMenu Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ContextMenu Class

Updated: February 2009

Represents a pop-up menu that enables a control to expose functionality that is specific to the context of the control.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
Public Class ContextMenu _
    Inherits MenuBase
Visual Basic (Usage)
Dim instance As ContextMenu
C#
public class ContextMenu : MenuBase
Visual C++
public ref class ContextMenu : public MenuBase
JScript
public class ContextMenu extends MenuBase
XAML Object Element Usage
<ContextMenu>
  Items
</ContextMenu>

Content Model:  ContextMenu is a ItemsControl. Its content properties are Items and ItemsSource. For more information on the content model for ContextMenu, see Controls Content Model Overview.

The properties of the ContextMenu class are used to define the position and behavior of the ContextMenu.

A FrameworkElement or FrameworkContentElement can have a ContextMenu. If you assign a ContextMenu to the FrameworkElement..::.ContextMenu or FrameworkContentElement..::.ContextMenu property, the ContextMenuService class handles context menu operations in response to user interaction. Part of the work the ContextMenuService does is opening the ContextMenu when the user clicks the right mouse button over the control.

The following properties are defined by the ContextMenu and ContextMenuService classes. If any of these properties are set on both ContextMenu and the ContextMenuService, the property value from the ContextMenuService is used.

A ContextMenu is automatically placed inside a Popup control. This behavior cannot be changed.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

The following examples show how to create a ContextMenu.

<Button Name="cmButton" Height="30">
  Button with Context Menu
  <Button.ContextMenu>
    <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"/>
      </MenuItem>
    </ContextMenu>
  </Button.ContextMenu>
</Button>

Visual Basic
Dim btn As New Button()
Dim contextmenu As New ContextMenu()
Dim mi As New MenuItem()
Dim mia As New MenuItem()

btn.Background = Brushes.Red
btn.Height = 30
btn.Content = "Created with Visual Basic."

mi.Header = ("Item 1")
contextmenu.Items.Add(mi)
mia.Header = ("Item 2")
contextmenu.Items.Add(mia)

btn.ContextMenu = (contextmenu)
cv2.Children.Add(btn)

C#
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";
mi.Items.Add(mia);
mib = new MenuItem();
mib.Header = "Open";
mi.Items.Add(mib);
mib1 = new MenuItem();
mib1.Header = "Recently Opened";
mib.Items.Add(mib1);
mib1a = new MenuItem();
mib1a.Header = "Text.xaml";
mib1.Items.Add(mib1a);
contextmenu.Items.Add(mi);
cv2.Children.Add(btn);

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.Control
              System.Windows.Controls..::.ItemsControl
                System.Windows.Controls.Primitives..::.MenuBase
                  System.Windows.Controls..::.ContextMenu
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

February 2009

Described how default styles change dependency properties.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
event handling documentation      kitntodd   |   Edit   |   Show History
It's easy to find examples on how to create a ContextMenu. But I can't find any documentation on how to handle events. The ContextMenu is an ItemsControl and can be databound. Is it possible to handle click events on MenuItem objects that are created as a result of databinding?
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker