In the previous article, we completed the development of the shortcut toolbar. This article will explain the applicationProgramMenu development. As shown in, click the application button icon in the upper-left corner of the program window to display the application menu list. The buttons in the list are some basic functions of the software.
Ribboncommand
Taking the "open" button as an example, you still need to define its <ribboncommand> content in <ribbonwindow. Resources>.
< R : Ribboncommand X : Key = "Opencommand" Labeltitle = "Open" Canexecute = "Opencommand_canexecute" Executed = "Opencommand_executed" Smallimagesource = "Images/open.png" Largeimagesource = "Images/open.png" Tooltiptitle = "Open"
Tooltipdescription = "Open Document"/>
Add a command event for <ribboncommand> to enable the document function:
Private void Opencommand_canexecute (Object Sender, Canexecuteroutedeventargs E) {e. canexecute = True ;} Private void Opencommand_executed ( Object Sender, Executedroutedeventargs E ){ Shellcontainer SC = Knownfolders . Documentslibrary As Shellcontainer ; Commonopenfiledialog Cofd = New Commonopenfiledialog (); Cofd. initialdirectoryshellcontainer = SC; cofd. defaultextension = ". Txt" ; Cofd. Filters. Add ( New Commonfiledialogfilter ( "Text files" , "*. Txt" )); If (Cofd. showdialog () = Commonfiledialogresult . OK) {txtbox. Text = File . Readalltext (cofd. filename, Encoding . Default );}}
Applicationmenu
<Ribboncommand> after completion, add <ribbonapplicationmenu> in <ribbon> to set the content in the menu list. <Ribbonapplicationmenuitem> is the menu button, and the corresponding <ribboncommand> is added to the command attribute. In addition, keys can be separated by <separator>.
< R : Ribbon Dockpanel. Dock = "TOP" Focusmanager. isfocusscope = "True" Title = "Wpf4 Notepad"> < R : Ribbon. applicationmenu > < R : Ribbonapplicationmenu Command = "{ Staticresource Appmenucommand } "> < R : Ribbonapplicationmenuitem Command = "{ Staticresource Opencommand } "/> < R : Ribbonapplicationmenuitem Command = "{ Staticresource Savecommand } "/> < Separator /> < R : Ribbonapplicationsplitmenuitem Command = "{ Staticresource Sendascommand } "> < R : Ribbonapplicationmenuitem Command = "{ Staticresource Mailcommand } "/> < R : Ribbonapplicationmenuitem Command = "{ Staticresource Twittercommand } "/> </ R : Ribbonapplicationsplitmenuitem > < Separator /> < R : Ribbonapplicationmenuitem Command = "{ Staticresource Closecommand } "/> </ R : Ribbonapplicationmenu > </ R : Ribbon. applicationmenu > </ R : Ribbon >
AboveCodeYou can use <ribbonapplicationsplitmenuitem> to extend the buttons (for example, the sendas button) that have sub-menus. You can use the labeldescription attribute of <ribboncommand> to set the content of the sub-menu title (as shown in the following code ).
< R : Ribboncommand X : Key = "Sendascommand" Labeltitle = "Sendas" Labeldescription = "Send this text to the world" Canexecute = "Sendascommand_canexecute" Smallimagesource = "Images/sendas.png" Largeimagesource = "Images/sendas.png" Tooltiptitle = "Sendas" Tooltipdescription = "Send this text to the world"/>
Applicationbutton
Finally, complete the development of the application menu icon (Notepad icon. Of course, you also need to set through <ribboncommand>. The difference is that you do not need to add canexecute and executed content.
r : ribboncommand x : key =" appmenucommand " labeltitle =" application button " smallimagesource =" images/notepad.png " largeimagesource =" images/notepad.png " tooltiptitle =" wpf4 Notepad " tooltipdescription =" Notepad application with ribbon sample "/>
After <ribboncommand> is added to the <ribbonapplicationmenu> command attribute, the style is displayed by default. The icon is not in the same shape as that in Office 2007.
The ribbon control library provides three style templates: office2007black, office2007blue, and office2007silver () to achieve the circular effect and different ribbon styles.
PublicMainwindow () {initializecomponent ();This. Resources. mergeddictionaries. Add (Popularapplicationskins. Office2007black );}
This article introduces the development of the application menu. The next article will officially introduce the development of the tab toolbar. In addition, this exampleSource codeIt will also be published together. Stay tuned... ...