Providing extra functionalities using the context menu
The context menu provides a vital role in any Windows applications offering additional functionalities to the user, within that context. This is often done relevant to a single control or a window.
When you right-click on a control or a window, you can provide a popup context menu to the user, to perform single-click actions. WPF provides a ContextMenu
property to all framework elements to hold a ContextMenu
, having hierarchical MenuItems
.
Consider this recipe to learn more about adding a context menu in your WPF application.
Getting ready
Create a new project named CH02.ContextMenuDemo
, using the WPF application project template of Visual Studio.
How to do it...
Follow these steps to add a context menu to a TextBlock
control. The same steps can be followed to add a context menu to any of the controls inheriting FrameworkElement
:
- Open the
MainWindow.xaml
file to modify the application UI. - Replace the entire
Grid
block with the following XAML...