BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Menu Controls - ContextMenu

The seventy-eighth part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the WPF menu controls. This article describes the ContextMenu control, which provides pop-up menus linked to other WPF controls.

ContextMenu

In the last instalment of the WPF tutorial we saw the MenuItem control, which we used to define single items in a Menu, or to act as a submenu containing further commands in a hierarchical structure. In this article we'll look at creating context-sensitive menus with the ContextMenu control.

Context-sensitive menus are pop-up menus that are linked to another control. When the user right-clicks the linked control, the menu appears. The user can then select a command from the menu or click elsewhere on the screen to hide it.

To demonstrate, we need a sample project. Create a new WPF application solution in Visual Studio named, "ContextMenuDemo". Once ready, replace the XAML in the main window with the following code:

<Window x:Class="ContextMenuDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ContextMenu Demo"
        Height="200"
        Width="250">
    <Grid>
        <TextBox Name="MyTextBox" Margin="4" TextWrapping="Wrap" />
    </Grid>
</Window>

Run the program to see the results. The generated window includes a single TextBox. If you right-click the TextBox you'll see the default context-sensitive menu, which contains menu items for cutting, copying and pasting text. We will override this menu.

WPF ContextMenu demonstration window

Adding a Context-Sensitive Menu to a Control

You can add a context-sensitive menu to a control by setting its ContextMenu property, which is defined in the FrameworkElement base class. This is set to a ContextMenu containing the menu items that you wish to appear. The menu items are defined in the same manner as for a Menu control and can include submenus, icons, access keys and input gestures, as we've seen in the previous article.

To set a context menu using XAML, you must use property element syntax. Let's replace the text box's menu with one that contains two items. Modify the XAML for the TextBox as follows:

<TextBox Name="MyTextBox" Margin="4" TextWrapping="Wrap">
    <TextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Clear" Click="Clear_Click" />
            <MenuItem Header="Select All" InputGestureText="Ctrl+A" Click="SelectAll_Click" />
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

You can see that the code includes two MenuItems. The first will be used to clear the content of the TextBox. The second, which includes an input gesture that matches the standard command for the TextBox, will select all of the text in the control.

To set up the functionality of the menu items, add the following code behind the window:

private void Clear_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.Clear();
}

private void SelectAll_Click(object sender, RoutedEventArgs e)
{
    MyTextBox.SelectAll();
}

Run the program and add some text to the text box. Right click the control and try the two menu options.

WPF ContextMenu on TextBox

Miscellaneous Members

ContextMenu includes a number of additional properties and events. A group of these provide identical functionality as for the ToolTip control, which we looked at in an earlier article. These are the placement and drop shadow properties, and the Opened and Closed events. I won't describe these members again. For more information read the ToolTip article.

12 July 2014