Creating a context menu
Context menus are typically invoked when the user right clicks a certain area or element (or presses Shift + F10 or a designated key on some keyboards). They are called "context" menus because different situations may call for different menus, or at least different available options. The ContextMenu
class (a derivative of MenuBase
) can host a bunch of MenuItem
controls (and separators). Let's see how this works.
Getting ready
Open the CH04.Lists
project from the Creating a list of items recipe.
How to do it…
We'll continue with the CH04.Lists
project and enhance it to make a context menu available:
Open
MainWindow.xaml
.Set the
ContextMenu
property of theListBox
to aContextMenu
object with someMenuItem
instances:<ListBox.ContextMenu> <ContextMenu> <MenuItem Header="_Language" > <MenuItem Header="C++" Tag="0" /> <MenuItem Header="C#" Tag="1" /> <MenuItem Header="Visual Basic" Tag="2" />...