What is WPF Menu?

  • The menu control in WPF is a collection of menu items. Each menu item is associate with commands. Menu items may contain children menu items it is called submenu.
  • Menu Control is hierarchically organized elements associated with the commands and event handler. it is item control which contains a collection of object types such as string, image, etc.
  • Menu control is created using menu and menu items.

Syntax:

Let’s create Menu using XAML

Here we will create a menu with menu item File and Edit. In the menu item Header includes the name of the menu item. Edit contains sub menus that are Save As and Delete. Sub menus define using MenuItem tag in the Edit menu item.

Now let’s create a menu at Run-time or using code behind.

When we are creating a menu at run-time it will add the menu dynamically. We have to create an object of a menu item and add a menu item object to the Main menu, and for sub menu add an object of the submenu to the corresponding menu item.

Here we are creating a menu the same as created using XAML.

Here, Object of menu item added to the main menu and objects of submenus of Edit added to the menu item Edit.

In the above code, we have created a menu dynamically. Dynamic means which is not fixed or static.

Example

We are creating one simple WPF application with a dynamic menu in which we will bind menus. Here we have created menu items File, Edit, View, and Tool. All menu items and sub-menu items are bind by MenuItems. File and Edit have sub-menu. The file contains 3 more sub-menus New, Open, and Close. New and Open contains submenus New has Project/Solution and Folder and Open has Project/Solution, Repository, and File. Edit contains more sub-menu Cut, Copy and paste. 

XAML file

CS file

Image : Menu Screen

Now we are creating one more application in which on button click event 2 menus will add to our application. Here, we have created one button “Add Menu” and on button click, we are adding menus at run time.

In the button click method, we have created a Main menu and set that Main menu to the Grid, using that Main menu object of Menu Item added to the Main menu. File and Edit menu items will add when a button is clicked. For the submenu, we have added the object of the submenu to the menu item.

XAML File

Image : Main Window (Before Click)

CS File

Image: Main Window (After Click)

Conclusion:

When we are creating a menu at run time it is called a dynamic menu. In WPF when we are creating a menu using code behind or binding menu, Menus is created dynamically. We hope you will acquire a clear idea regarding the Dynamic menus in detail.

Author Bio: Ajay Patel – Technical Director, iFour Technolab Pvt Ltd

Seasoned technocrat with years of experience building technical solutions for various industries using Microsoft technologies. Wish sharp understanding and technical acumen, have delivered hundreds of Web, Cloud, Desktop and Mobile solutions and is heading the technical department at WPF development Company – iFour Technolab Pvt Ltd.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.