WPF - Controls-Menu - Windows Presentation Foundation(WPF)

What is WPF Menu?

Menu is a control that enables you to hierarchically organize elements associated with the commands and event handlers. Menu is an ItemsControl, so it can contain a collection of any object type such as string, image, or panel. The hierarchical inheritance of Menu class is as follows −

hierarchy_of_menu

Below are the commonly used properties on Menu class

Given below are the most commonly used properties of Menu.

Sr.No. Property & Description
1
Background
Gets or sets a brush that describes the background of a control. (Inherited from Control.)
2
BindingGroup
Gets or sets the BindingGroup that is used for the element. (Inherited from FrameworkElement.)
3
BitmapEffect
Obsolete. Gets or sets a bitmap effect that applies directly to the rendered content for this element. This is a dependency property. (Inherited from UIElement.)
4
BorderThickness
Gets or sets the border thickness of a control. (Inherited from Control.)
5
ContextMenu
Gets or sets the context menu element that should appear whenever the context menu is requested through user interface (UI) from within this element. (Inherited from FrameworkElement.)
6
Effect
Gets or sets the bitmap effect to apply to the UIElement. This is a dependency property. (Inherited from UIElement.)
7
Height
Gets or sets the suggested height of the element. (Inherited from FrameworkElement.)
8
IsMainMenu
Gets or sets a value that indicates whether this Menu receives a main menu activation notification.
9
Items
Gets the collection used to generate the content of the ItemsControl. (Inherited from ItemsControl.)
10
ItemsPanel
Gets or sets the template that defines the panel that controls the layout of items. (Inherited from ItemsControl.)
11
ItemsSource
Gets or sets a collection used to generate the content of the ItemsControl. (Inherited from ItemsControl.)
12
ItemStringFormat
Gets or sets a composite string that specifies how to format the items in the ItemsControl if they are displayed as strings. (Inherited from ItemsControl.)
13
ItemTemplate
Gets or sets the DataTemplate used to display each item. (Inherited from ItemsControl.)
14
ToolTip
Gets or sets the tool-tip object that is displayed for this element in the user interface (UI). (Inherited from FrameworkElement.)
15
VerticalContentAlignment
Gets or sets the vertical alignment of the control's content. (Inherited from Control.)
16
Width
Gets or sets the width of the element. (Inherited from FrameworkElement.)

Commonly Used Events in Menu Class

Given below are the most commonly used events of Menu.

Sr.No. Event & Description
1
ContextMenuClosing
Occurs just before any context menu on the element is closed. (Inherited from FrameworkElement.)
2
ContextMenuOpening
Occurs when any context menu on the element is opened. (Inherited from FrameworkElement.)
3
KeyDown
Occurs when a key is pressed while focus is on this element. (Inherited from UIElement.)
4
KeyUP
Occurs when a key is released while focus is on this element. (Inherited from UIElement.)
5
ToolTipClosing
Occurs just before any tooltip on the element is closed. (Inherited from FrameworkElement.)
6
ToolTipOpening
Occurs when any tooltip on the element is opened. (Inherited from FrameworkElement.)
7
TouchDown
Occurs when a finger touches the screen while the finger is over this element. (Inherited from UIElement.)
8
TouchEnter
Occurs when a touch moves from outside to inside the bounds of this element. (Inherited from UIElement.)
9
TouchLeave
Occurs when a touch moves from inside to outside the bounds of this element. (Inherited from UIElement.)
10
TouchMove
Occurs when a finger moves on the screen while the finger is over this element. (Inherited from UIElement.)
11
TouchUp
Occurs when a finger is raised off of the screen while the finger is over this element. (Inherited from UIElement.)

Example

  • Let’s create a new WPF project with the name WPFMenuControl.
  • Drag a menu control from the Toolbox to the design window.

The following example contains three menu options with some menu items. When the user clicks an item, the program updates the title. Here is the XAML code −

Here is the events implementation in C#.

When you compile and execute the above code, it will produce the following output −

output_of_menu

We recommend that you execute the above example code and try the other properties and events of Menu class.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics