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: Create Menu
- 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);
- }
Step 5: Load Image Icon - Create Object of System.Windows.Controls.Image.
- Assign the “System.Windows.Media.ImageSource” Path to Image Object .
- Assign the Image object to the “Icon” Property of Menu Items.
- Image ObjImage = new Image();
- string path = ConfigurationManager.AppSettings["AppPath"].ToString() + "/Images/pic.png";
- ObjImage.Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
- mnuInvoices.Icon = ObjImage;
Reference
Uri: http://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx
UriKind : http://www.c-sharpcorner.com/Forums/Thread/242530/
WPF setting a MenuItem.Icon in code