1、首先需要 下載Ribbon For WPF.目前最新的版本是Microsoft Ribbon for WPF October 2010。
下載 鏈接:
https://www.microsoft.com/en-us/download/details.aspx?id=11877#filelist
下載 完成后安裝。
2、打開Visual Studio 2013創建WPF應用程序。添加程序集引用
"C:\Program Files (x86)\Microsoft Ribbon for WPF\V4.0\RibbonControlsLibrary.dll"
添加完成 后,將控件 程序集映射到XAML前綴
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
再在XAML中添加功能區控件
<r:Ribbon></r:Ribbon>
在添加內容前,我們需要先了解下RibbonWindow的結構:如圖
①快速訪問工具欄(QuickAccessToolBar)
①快速訪問工具欄(QuickAccessToolBar)
<r:Ribbon.QuickAccessToolBar> <r:RibbonQuickAccessToolBar> <r:RibbonButton SmallImageSource="/Images/Save.png" LargeImageSource="/Images/Save.png" ToolTipTitle="保存" ToolTipDescription="已其他形式保存該郵件" Command="SaveAs"/> <r:RibbonButton SmallImageSource="/Images/Undo.png" LargeImageSource="/Images/Undo.png" ToolTipTitle="撤銷" ToolTipDescription="已其他形式保存該郵件" Command="ApplicationCommands.Undo" /> <r:RibbonButton SmallImageSource="/Images/Redo.png" LargeImageSource="/Images/Redo.png" ToolTipTitle="恢復" ToolTipDescription="已其他形式保存該郵件" Command="ApplicationCommands.Redo"/> <r:RibbonButton SmallImageSource="/Images/attach.png" LargeImageSource="/Images/attach.png" ToolTipTitle="附件" ToolTipDescription="已其他形式保存該郵件" x:Name="quickAttachBtn" /> </r:RibbonQuickAccessToolBar> </r:Ribbon.QuickAccessToolBar>
這個是在窗口icon旁邊的快速訪問工具欄,主要是給一些比較常用的按鈕使用的。
②應用程序菜單(ApplicationMenu)
<ribbon:Ribbon.ApplicationMenu> <ribbon:RibbonApplicationMenu SmallImageSource="Images/ApplicationMenuIcon.png" KeyTip="F"> <ribbon:RibbonApplicationMenuItem Header="Open" Command="Open" ImageSource="Images/Open32.png" KeyTip="O" /> <ribbon:RibbonApplicationMenuItem Header="Save" Command="Save" ImageSource="Images/Save32.png" KeyTip="S" /> <ribbon:RibbonApplicationSplitMenuItem Header="Save As" Command="SaveAs" ImageSource="Images/SaveAs32.png" KeyTip="V" > <ribbon:RibbonApplicationMenuItem Header="Rich Text document" Command="SaveAs" CommandParameter="rtf" ImageSource="Images/SaveAsRtf32.png" KeyTip="R" /> <ribbon:RibbonApplicationMenuItem Header="Plain Text document" Command="SaveAs" CommandParameter="txt" ImageSource="Images/SaveAsTxt32.png" KeyTip="P" /> <ribbon:RibbonApplicationMenuItem Header="Other format" Command="SaveAs" ImageSource="Images/SaveAs32.png" KeyTip="O" /> </ribbon:RibbonApplicationSplitMenuItem> <ribbon:RibbonSeparator /> <ribbon:RibbonApplicationSplitMenuItem Header="Print" ImageSource="Images/Print32.png" KeyTip="R" /> <ribbon:RibbonApplicationMenuItem Header="Page Setup" ImageSource="Images/PrintSetup32.png" KeyTip="G" /> <ribbon:RibbonApplicationMenu.FooterPaneContent> <DockPanel LastChildFill="False"> <ribbon:RibbonButton Command="ApplicationCommands.Close" Label="Exit" ToolTipTitle="Exit" SmallImageSource="Images\Exit16.png" KeyTip="X" DockPanel.Dock="Right" Margin="2" BorderBrush="#B8114EAF" /> </DockPanel> </ribbon:RibbonApplicationMenu.FooterPaneContent> <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent> <ribbon:RibbonGallery CanUserFilter="False" ScrollViewer.VerticalScrollBarVisibility="Auto"> <ribbon:RibbonGalleryCategory Header="Recent Documents" Background="Transparent" ItemsSource="{DynamicResource MostRecentFiles}"> <ribbon:RibbonGalleryCategory.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" IsItemsHost="True"/> </ItemsPanelTemplate> </ribbon:RibbonGalleryCategory.ItemsPanel> </ribbon:RibbonGalleryCategory> </ribbon:RibbonGallery> </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent> </ribbon:RibbonApplicationMenu> </ribbon:Ribbon.ApplicationMenu>
③選項卡(RibbonTab)和組(RibbonGroup)
<r:RibbonTab x:Name="HomeTab" Header="Home"> <r:RibbonGroup x:Name="Group1" Header="Group1"> <r:RibbonGroup.GroupSizeDefinitions> <r:RibbonGroupSizeDefinition> <r:RibbonControlSizeDefinition ImageSize="Large" /> <r:RibbonControlSizeDefinition ImageSize="Small" /> <r:RibbonControlSizeDefinition ImageSize="Small" /> </r:RibbonGroupSizeDefinition> </r:RibbonGroup.GroupSizeDefinitions> <r:RibbonButton x:Name="Button3" LargeImageSource="Images\LargeIcon.png" Label="Button1" /> <r:RibbonButton x:Name="Button1" SmallImageSource="Images\SmallIcon.png" Label="Button3" /> <r:RibbonButton x:Name="Button2" SmallImageSource="Images\SmallIcon.png" Label="Button4" /> </r:RibbonGroup> <r:RibbonGroup> <r:RibbonButton x:Name="Button5" LargeImageSource="Images\LargeIcon.png" Label="Button1" Click="Button5_Click" /> <r:RibbonButton x:Name="Button6" SmallImageSource="Images\SmallIcon.png" Label="Button2" /> </r:RibbonGroup> </r:RibbonTab>
要讓Group符合我們的想法,先要使用GroupSizeDefinitions對Group內的布局進行設置(當然不設置也是可以的,不過當我們講到后面的讓Ribbon隨着應用的大小自動調整的時候就要使用到GroupSizeDefinitions了),使用ImageSize指定對應位置控件的大小,使用IsLabelVisible對控件的Lable是否可見進行設置。