Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ContextMenu Class

Represents a Menu control that contains a pop-up menu. The ContextMenu allows a control to expose functionality through the use of a Menu that is specific to the context of the control.

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

Public Class ContextMenu
	Inherits MenuBase
Dim instance As ContextMenu

public class ContextMenu extends MenuBase
public class ContextMenu extends MenuBase

If you programmatically set the ContextMenuIsOpen property to true before initializing a ContextMenu, the Opened event does is not raised until the user opens the menu.

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

ContentModel: A ContextMenu is an ItemsControl. For more information on the content model for ContextMenu, see ItemsControl Content Model.

The following examples show how to create ContextMenu controls using Extensible Application Markup Language (XAML) and C#.

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

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")
mia.Header = ("Item 2")

btn.ContextMenu = (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 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

© 2015 Microsoft