WPF initializing menus in MainWindow

Source: Internet
Author: User

1. Define a menu model class to hold the key, title, and submenu _submenus,formfullname is used to save the corresponding control of the menu.

The 2.MainWindow construction method adds an assembly to the flood forecast service.

Initialize the system menu here, Save the System main menu with List<qlmenu> (the main menu is displayed with ToggleButton), the submenu with the Formfullname attribute is associated with the window displayed with the TabControl, followed by a special method.

3. The main form load gets a collection of all the menus-a recursive list, and then initializes each menu.

Submenus have a level menu, a level two menu, and so on. The first level menu is a large category, shown at the top.

Pnlcaption is a stackpanel that is used to store a level menu, ToggleButton is a base class for a switchable state control, such as a CheckBox, which is used here as a UI display for a first-level menu, if the menu is selected, ToggleButton has the corresponding style conversion display effect.

The input parameter is the collection of all the menu items we want to use in the previous step, where we loop to get the first level menu, the Red box section easily links the Tooglebutton with the custom menu class (We have no menu class defined any common menu functions from start to finish, we use the button to represent the menu button, The DataContext of the button saves the menu item and its submenu item (submenus), which is the equivalent of our own system's menu function (WPF has a menu class, should have it).

The last Btndefault_click (Defaultbtn,null) simulates the event that the menu item is pressed, noting that DEFAULTBTN is identified as the first level menu in the loop, so that when the system menu is initialized, The first menu item added will be selected when the program is initially loaded.

First of all, to initialize the level two menu, initialize the level two menu after clicking on the button of the first menu, the incoming parameter is the submenus of the button's DataContext level menu. This adds a menu tag to the View Designer, which is the framework's menu class (System.Windows.Controls.Menu). Item.click + = new Routedeventhandler (menuitem_click) Adds an event delegate to the level two menu, and when you click a menu item, you can do what we want to do in the delegate's associated method, That is, loading a submenu-related subform (in two---loading a single subform (which might contain a TabControl control) or a submenu below the menu, the second case is implemented by the specified method after judgment, and this is done in tabpage form), Qlmenu is a data entity that preserves our menu data and relationships, MenuItem and ToggleButton above are interface elements that we use with item. DataContext and Btn.datacontext are very elegant in connection with the data model, which also conforms to the WPF programming philosophy. In the end, of course, the first menu item is still tactfully selected.

WPF initializing menus in MainWindow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.