In this document we are discussing about how to generate WPF menu dynamically in visual studio 2012.
Step 1: Create New Project.
Step 2: Add Menu Control to The grid.
Step 3: Include “System.Windows.Controls” in the project.
- Create object for Menuitem.
- Edit the properties of MenuItem.
- Add Menu item to the Main Menu.
- MenuItem mnuInvoices = new MenuItem();
-
- mnuInvoices.Header = "Invoice";
-
- mnuInvoices.Height = 50;
-
- this.mnuMain.Items.Add(mnuInvoices);
Step 4: Create Sub Menu
- Create object for Menuitem.
- Edit the properties of MenuItem.
- And add the submenu object to the corresponding MenuItem.
- MenuItem mnuInvoices = new MenuItem();
-
- mnuInvoices.Header = "Invoice";
-
- mnuInvoices.Height = 50;
-
- this.mnuMain.Items.Add(mnuInvoices);
-
- if (ObjUsr.UserTypeid == 1)
-
- {
-
- MenuItem mnuDeleteInvoice = new MenuItem();
-
- mnuDeleteInvoice.Header = "Delete Invoice";
-
- mnuDeleteInvoice.Height = 50;
-
- mnuInvoices.Items.Add(mnuDeleteInvoice);
-
- MenuItem mnuInvoiceStatus = new MenuItem();
-
- mnuInvoiceStatus.Header = "Invoice Status";
-
- mnuInvoiceStatus.Height = 50;
-
- mnuInvoices.Items.Add(mnuInvoiceStatus);
-
- }
-
- else
-
- {
-
- MenuItem mnuInvoiceStatus = new MenuItem();
-
- mnuInvoiceStatus.Header = "Invoice Status";
-
- mnuInvoiceStatus.Height = 50;
-
- mnuInvoices.Items.Add(mnuInvoiceStatus);
-
- }