添加和自定義工具欄皮膚選擇器
您可以在工具欄(BarManager)和Ribbon Control中添加皮膚選擇器,并允許用戶在運行時選擇皮膚。
將皮膚選擇器添加到工具欄
在設計時,單擊工具欄中的Add按鈕,并從skin Item子菜單中選擇一個皮膚選擇器。
可以使用以下皮膚選擇器:
Skin Menu (SkinBarSubItem)—— 將外觀名稱顯示為菜單。
Skin List (SkinDropDownButtonItem)————將皮膚名稱顯示為可滾動列表,并帶有內(nèi)置搜索框,可按名稱查找皮膚。
Skin Palette List (SkinPaletteDropDownButtonItem)—— 顯示當前選定的矢量皮膚的調(diào)色板。
您可以在代碼中修改皮膚選擇器。為此請使用外觀選擇器的BarCustomContainerItem.ItemLinks集合。
當您創(chuàng)建外觀選擇器(SkinBarSubItem、SkinDropDownButtonItem、SkinPaletteDropDownButtonItem、SkinRibbonGalleryBarItem和SkinPaletteRibbonGalleryBarItem)并將其添加到代碼中的工具欄或功能區(qū)控件時,請確保調(diào)用外觀選擇器的Initialize方法。
C#:
SkinBarSubItem skinBarSubItem1 = new SkinBarSubItem(); barManager1.Items.Add(skinBarSubItem1); bar1.ItemLinks.Add(skinBarSubItem1); skinBarSubItem1.Initialize();
VB.NET:
Dim skinBarSubItem1 As New SkinBarSubItem() BarManager1.Items.Add(skinBarSubItem1) Bar1.ItemLinks.Add(skinBarSubItem1) skinBarSubItem1.Initialize()
隱藏皮膚和皮膚組
您可以隱藏皮膚選擇器中的單個項目,如下所示:
1.創(chuàng)建要排除的皮膚名稱的字符串數(shù)組,您可以使用完整名稱(例如“Office 2016 Colorful”)或部分名稱(例如“2007”)。
C#:
string[] skinsToHide = { "Seven Classic", "DevExpress Style", "Dark", "2010", "2007", "Sharp" };
VB.NET:
Dim skinsToHide() As String = { "Seven Classic", "DevExpress Style", "Dark", "2010", "2007", "Sharp" }
2.創(chuàng)建一個自定義方法 ( HideSkins ),用于迭代皮膚選擇器的項目,并刪除與數(shù)組中的值匹配的皮膚。
C#:
private void HideSkins(string[] skinsToHide) { for(var i = 0; i < skinBarSubItem1.ItemLinks.Count; i++) { //Check regular button items if(skinBarSubItem1.ItemLinks[i] is BarButtonItemLink) { var item = skinBarSubItem1.ItemLinks[i]; foreach(var skin in skinsToHide) { if(item.Caption.Contains(skin)) { item.Visible = false; } } } //Check buttons nested in the "Bonus Skins" sub-menu if(skinBarSubItem1.ItemLinks[i] is BarSubItemLink && skinBarSubItem1.ItemLinks[i].Caption == "Bonus Skins") { BarSubItemLink group = (BarSubItemLink)skinBarSubItem1.ItemLinks[i]; for(var j = 0; j < group.Item.ItemLinks.Count; j++) { var item = group.Item.ItemLinks[j]; foreach(var skin in skinsToHide) { if(item.Caption.Contains(skin)) { item.Visible = false; } } } } //Hide theme skins if(skinBarSubItem1.ItemLinks[i] is BarSubItemLink && skinBarSubItem1.ItemLinks[i].Caption == "Theme Skins") { skinBarSubItem1.ItemLinks[i].Visible = false; } } }
VB.NET:
Private Sub HideSkins(ByVal skinsToHide() As String) For i = 0 To skinBarSubItem1.ItemLinks.Count - 1 'Check Items If TypeOf skinBarSubItem1.ItemLinks(i) Is BarButtonItemLink Then Dim item = skinBarSubItem1.ItemLinks(i) For Each skin In skinsToHide If item.Caption.Contains(skin) Then item.Visible = False End If Next skin End If 'Check bonus skins If TypeOf skinBarSubItem1.ItemLinks(i) Is BarSubItemLink AndAlso skinBarSubItem1.ItemLinks(i).Caption <> "Theme Skins" Then Dim group As BarSubItemLink = CType(skinBarSubItem1.ItemLinks(i), BarSubItemLink) For j = 0 To group.Item.ItemLinks.Count - 1 Dim item = group.Item.ItemLinks(j) For Each skin In skinsToHide If item.Caption.Contains(skin) Then item.Visible = False End If Next skin Next j End If 'Hide theme skins If TypeOf skinBarSubItem1.ItemLinks(i) Is BarSubItemLink AndAlso skinBarSubItem1.ItemLinks(i).Caption = "Theme Skins" Then skinBarSubItem1.ItemLinks(i).Visible = False End If Next i End Sub
3.從Load事件處理程序調(diào)用HideSkins方法。
C#:
void ucBar_Load(object sender, EventArgs e) { HideSkins(skinsToHide); }
VB.NET:
Private Sub ucBar_Load(ByVal sender As Object, ByVal e As EventArgs) HideSkins(skinsToHide) End Sub
更改標題和圖標
用迭代皮膚選擇器的ItemLinks集合來更改項目標題或字形。
C#:
void ucBar_Load(object sender, EventArgs e) { RenameSkins(); } private void RenameSkins() { for(var i = 0; i < skinBarSubItem1.ItemLinks.Count; i++) { if(skinBarSubItem1.ItemLinks[i].Caption == "DevExpress Style") { BarButtonItem parentItem = (BarButtonItem)skinBarSubItem1.ItemLinks[i].Item; parentItem.Caption = "Default Skin"; parentItem.ImageUri.Uri = "Apply"; } } }
VB.NET:
Private Sub ucBar_Load(ByVal sender As Object, ByVal e As EventArgs) RenameSkins() End Sub Private Sub RenameSkins() For i = 0 To skinBarSubItem1.ItemLinks.Count - 1 If skinBarSubItem1.ItemLinks(i).Caption = "DevExpress Style" Then Dim parentItem As BarButtonItem = CType(skinBarSubItem1.ItemLinks(i).Item, BarButtonItem) parentItem.Caption = "Default Skin" parentItem.ImageUri.Uri = "Apply" End If Next i End Sub
下圖顯示了結(jié)果:
使用定位器更改皮膚標題
您還可以使用Localizer對象來重命名外觀項目。