In This Article

ContextMenu

The ContextMenu class provides functionality for supporting a Menu of ribbon control items in a standard context menu.

Many of the interactive ribbon controls are capable of being hosted in a context menu, via use within a Menu.

Screenshot

A sample ribbon context menu used for an edit control

Compatibility with Native WPF ContextMenu

Since the ribbon ContextMenu class inherits the native WPF ContextMenu class, the ribbon ContextMenu may be used anywhere that a regular context menu can be used.

We recommend using the ribbon ContextMenu on any windows that have a ribbon user interfaceso that the UI experience is consistent for end users.

Using Menu as a ContextMenu Child

The Menu control should be used as the direct child of a ribbon ContextMenu. Then add ribbon controls that support menus as children of the Menu.

The Menu topic discusses which controls are best used within that control. Also, see the sample XAML below that shows how the ContextMenu in the screenshot above was created.

Keeping the ContextMenu Displayed When an Item is Clicked

Each ButtonBase defines a StaysOpenOnClick property that governs whether the containing menu closes when the menu item is clicked.

The default value for this property is false but can optionally be set to true.

Sample XAML

This code sample shows how to easily define a context menu for a RichTextBox in XAML.

<RichTextBox>
	<RichTextBox.ContextMenu>
		<ribbon:ContextMenu>
			<ribbon:Menu>
				<ribbon:Button Command="ApplicationCommands.Undo" />
				<ribbon:Button Command="ApplicationCommands.Redo" />
				<ribbon:Separator />
				<ribbon:Button Command="ApplicationCommands.Cut" />
				<ribbon:Button Command="ApplicationCommands.Copy" />
				<ribbon:Button Command="ApplicationCommands.Paste" />
			</ribbon:Menu>
		</ribbon:ContextMenu>
	</RichTextBox.ContextMenu>
</RichTextBox>

Keyboard Shortcut Display

Each ButtonBase defines a InputGestureText property that indicates the keyboard shortcut text to display when in a small menu item context.

If a command assigned to a menu item has a KeyGesture, its text will be used by default. However, you can override this value or set a custom one when not using a command by setting the InputGestureText property directly.

Adding Access Keys to Menu Items

When any sort of ribbon button (Button, PopupButton, etc.) is used in a ContextMenu as a menu item, an underline will appear under a character in the menu item's label if the control's KeyTipAccessText property has been set to a single alpha/numeric character and that character appears within the control's label. While the context menu is displayed the access key can be typed to execute the default action on the control.

For instance, a PasteButton that had a label of Paste and a KeyTipAccessText of P would have the P in Paste underlined.