在上一篇中我們完成了快捷工具欄的開發(fā),本篇將講解應(yīng)用程序菜單開發(fā)的相關(guān)內(nèi)容。如下圖所示,點(diǎn)擊程序窗口左上角的記事本圖標(biāo)(Application Button)會顯示出應(yīng)用程序菜單(Application Menu)列表,列表中的按鍵即為軟件的一些基本功能。 RibbonCommand以“Open”按鍵為例,首先仍然需要在<RibbonWindow.Resources>中定義其<RibbonCommand>內(nèi)容。 <r:RibbonCommand x:Key="OpenCommand" LabelTitle="Open" CanExecute="OpenCommand_CanExecute" Executed="OpenCommand_Executed" SmallImageSource="Images/Open.png" LargeImageSource="Images/Open.png" ToolTipTitle="Open" 為<RibbonCommand>添加Command 事件實(shí)現(xiàn)打開文檔功能: 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>完成后繼續(xù)在<Ribbon>中添加<RibbonApplicationMenu>用于設(shè)置菜單列表中的內(nèi)容。其中<RibbonApplicationMenuItem>即為菜單按鍵,將相應(yīng)的<RibbonCommand>添加到Command 屬性中。另,按鍵之間可用<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> 上面代碼中對于存在子菜單的按鍵(例如,SendAs 按鍵)可用<RibbonApplicationSplitMenuItem>對其進(jìn)行擴(kuò)展。子菜單標(biāo)題內(nèi)容可通過<RibbonCommand>的LabelDescription 屬性進(jìn)行設(shè)置(如下代碼)。 <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最后來完成應(yīng)用程序菜單圖標(biāo)(記事本圖標(biāo))的開發(fā)。當(dāng)然也需要通過<RibbonCommand>進(jìn)行設(shè)置,與之前不同之處在于不用添加CanExecute 和Executed 內(nèi)容。 <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" /> 將<RibbonCommand>加入<RibbonApplicationMenu> Command 屬性后默認(rèn)情況呈現(xiàn)下圖樣式,圖標(biāo)的形狀并不與Office 2007 一樣為圓形。 如果想要圓形效果其實(shí)也很簡單,Ribbon 控件庫為我們提供了三種樣式模板:Office2007Black、Office2007Blue、Office2007Silver,只需在MainWindow() 中加入一行代碼即可實(shí)現(xiàn)圓形效果和不同的Ribbon 樣式。 public MainWindow() { InitializeComponent(); this.Resources.MergedDictionaries.Add(PopularApplicationSkins.Office2007Black); }
本篇關(guān)于應(yīng)用程序菜單的開發(fā)就介紹到這里,下篇將正式進(jìn)行標(biāo)簽工具欄(Tab Toolbar)的開發(fā)內(nèi)容。同時(shí)本示例源代碼也將一同公布。敬請關(guān)注… … 原文鏈接:http://www.cnblogs.com/gnielee/archive/2010/05/11/wpf4-ribbon-application-menu.html |
|