(0) exportieren Drucken
Alle erweitern

ContextMenu-Klasse

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-Namespaces:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public class ContextMenu : MenuBase
public class ContextMenu extends MenuBase
public class ContextMenu extends MenuBase
<ContextMenu>
  Items
</ContextMenu>

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

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

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Microsoft .NET Framework 3.0 wird unter Windows Vista, Microsoft Windows XP SP2 und Windows Server 2003 SP1 unterstützt.

.NET Framework

Unterstützt in: 3.0
Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
Anzeigen:
© 2014 Microsoft