Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.ContextMenu Property

Gets or sets the context menu element that should appear whenever the context menu is requested through user interface (UI) from within this element. This is a dependency property.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public ContextMenu ContextMenu { get; set; }
/** @property */
public ContextMenu get_ContextMenu ()

/** @property */
public void set_ContextMenu (ContextMenu value)

public function get ContextMenu () : ContextMenu

public function set ContextMenu (value : ContextMenu)

<object>
  <object.ContextMenu>
    <ContextMenu .../>
  </object.ContextMenu>
</object>

Property Value

The context menu assigned to this element.

Identifier field

ContextMenuProperty

Metadata properties set to true

None

ContextMenu itself is a FrameworkElement derived class, and it is technically possible for ContextMenu itself to have a ContextMenu property. However, this creates a confusing context menu experience for the user and this practice is not recommended.

This example describes how to create a ContextMenu.

The ContextMenu controls are connected to buttons and have a collection of MenuItem elements

<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>

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);

For the complete sample see ContextMenu Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

Show:
© 2014 Microsoft