構(gòu)建自定義最終用戶皮膚選擇器
本節(jié)介紹如何用DevExpress皮膚項(xiàng)填充ComboBoxEdit控件。
1.要填充組合框編輯器,迭代SkinManager.Skins集合(它返回所有當(dāng)前可用的DevExpress皮膚),并為每個(gè)適用的皮膚創(chuàng)建一個(gè)新的組合框項(xiàng)。如下面的代碼所示:
C#:
//Uncomment the following line to add bonus and theme skins //DevExpress.UserSkins.BonusSkins.Register(); foreach (SkinContainer cnt in SkinManager.Default.Skins) { comboBoxEdit1.Properties.Items.Add(cnt.SkinName); }
VB.NET:
'Uncomment the following line to add bonus and theme skins 'DevExpress.UserSkins.BonusSkins.Register() For Each cnt As SkinContainer In SkinManager.Default.Skins comboBoxEdit1.Properties.Items.Add(cnt.SkinName) Next cnt
您可以實(shí)現(xiàn)更復(fù)雜的邏輯來(lái)跳過(guò)或重命名特定的皮膚。例如,下面的代碼示例排除了“Caramel”皮膚和任何“Office 2007…”皮膚,并將“DevExpress Style”和“DevExpress Dark Style”皮膚分別重命名為“Default skin”和“Default Dark skin”。
C#:
DevExpress.UserSkins.BonusSkins.Register(); foreach (SkinContainer cnt in SkinM說(shuō)anager.Default.Skins) { if (cnt.SkinName.Contains("Office 2007") || cnt.SkinName == "Caramel") continue; if(!cnt.SkinName.Contains("DevExpress")) comboBoxEdit1.Properties.Items.Add(cnt.SkinName); else switch(cnt.SkinName) { case "DevExpress Style": comboBoxEdit1.Properties.Items.Add("Default Skin"); break; case "DevExpress Dark Style": comboBoxEdit1.Properties.Items.Add("Default Dark Skin"); break; } }
VB.NET:
DevExpress.UserSkins.BonusSkins.Register() For Each cnt As SkinContainer In SkinManager.Default.Skins If cnt.SkinName.Contains("Office 2007") OrElse cnt.SkinName = "Caramel" Then Continue For End If If Not cnt.SkinName.Contains("DevExpress") Then comboBoxEdit1.Properties.Items.Add(cnt.SkinName) Else Select Case cnt.SkinName Case "DevExpress Style" comboBoxEdit1.Properties.Items.Add("Default Skin") Case "DevExpress Dark Style" comboBoxEdit1.Properties.Items.Add("Default Dark Skin") End Select End If Next cnt
2.處理ComboBoxEdit.SelectedIndexChanged事件,以便在用戶選擇組合框項(xiàng)時(shí)應(yīng)用相應(yīng)的皮膚。
C#:
private void ComboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) { ComboBoxEdit comboBox = sender as ComboBoxEdit; string skinName = comboBox.Text; switch (skinName) { case "Default Skin": DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "DevExpress Style"; break; case "Default Dark Skin": DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "DevExpress Dark Style"; break; default: DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = skinName; break; } }
VB.NET:
Private Sub ComboBoxEdit1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim comboBox As ComboBoxEdit = TryCast(sender, ComboBoxEdit) Dim skinName As String = comboBox.Text Select Case skinName Case "Default Skin" DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "DevExpress Style" Case "Default Dark Skin" DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "DevExpress Dark Style" Case Else DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = skinName End Select End Sub
3.運(yùn)行應(yīng)用程序來(lái)查看結(jié)果。
SkinHelper方法
DevExpress.XtraBars.Helper.SkinHelper類提供了多個(gè)Init…方法,這些方法允許您使用皮膚/調(diào)色板項(xiàng)填充自定義控件。
在下面的圖中,兩個(gè)功能區(qū)按鈕是自定義皮膚和調(diào)色板選擇器,兩個(gè)按鈕的BarButtonItemButtonStyle屬性都等于DropDown。每個(gè)按鈕都有一個(gè)關(guān)聯(lián)的PopupControlContainer分配給BarButtonItemDropDownControl屬性,彈出控件容器承載的庫(kù)控件的Dock屬性設(shè)置為“填充”。
下面的代碼演示了如何使用InitSkinGallery和InitSkinPaletteGallery方法填充這些選擇器。
C#:
//Container and gallery settings popupControlContainer1.AutoSize = popupControlContainer2.AutoSize = true; popupControlContainer1.AutoSizeMode = popupControlContainer2.AutoSizeMode = AutoSizeMode.GrowAndShrink; galleryControl1.Gallery.AutoSize = galleryControl2.Gallery.AutoSize = GallerySizeMode.Both; //Populate the skin gallery galleryControl2.Gallery.RowCount = 6; SkinHelper.InitSkinGallery(galleryControl2); #region Optional Settings //Uncomment these lines to modify the gallery //---Enlarge item images /*foreach (GalleryItem item in galleryControl2.Gallery.GetAllItems()) item.ImageOptions.Image = item.ImageOptions.HoverImage; galleryControl2.Gallery.ImageSize = new Size(48, 48); galleryControl2.Gallery.AllowHoverImages = false;*/ //---Hide item and group captions /*galleryControl2.Gallery.ShowItemText = false; galleryControl2.Gallery.ShowGroupCaption = false;*/ //---Hide group selector /*galleryControl2.Gallery.AllowFilter = false;*/ //---Remove "Custom Skins" and "Theme Skins" groups /*galleryControl2.Gallery.Groups.RemoveAt(3); galleryControl2.Gallery.Groups.RemoveAt(2);*/ #endregion //Populate the palette gallery galleryControl1.Gallery.ColumnCount = 6; SkinHelper.InitSkinPaletteGallery(galleryControl1);
VB.NET:
'Container and gallery settings popupControlContainer2.AutoSize = True popupControlContainer1.AutoSize = popupControlContainer2.AutoSize popupControlContainer2.AutoSizeMode = AutoSizeMode.GrowAndShrink popupControlContainer1.AutoSizeMode = popupControlContainer2.AutoSizeMode galleryControl2.Gallery.AutoSize = GallerySizeMode.Both galleryControl1.Gallery.AutoSize = galleryControl2.Gallery.AutoSize 'Populate the skin gallery galleryControl2.Gallery.RowCount = 6 SkinHelper.InitSkinGallery(galleryControl2) '#Region "Optional Settings" 'Uncomment these lines to modify the gallery '---Enlarge item images 'foreach (GalleryItem item in galleryControl2.Gallery.GetAllItems()) ' item.ImageOptions.Image = item.ImageOptions.HoverImage; 'galleryControl2.Gallery.ImageSize = new Size(48, 48); 'galleryControl2.Gallery.AllowHoverImages = false; '---Hide item and group captions 'galleryControl2.Gallery.ShowItemText = false; 'galleryControl2.Gallery.ShowGroupCaption = false; '---Hide group selector 'galleryControl2.Gallery.AllowFilter = false; '---Remove "Custom Skins" and "Theme Skins" groups 'galleryControl2.Gallery.Groups.RemoveAt(3); 'galleryControl2.Gallery.Groups.RemoveAt(2); '#End Region 'Populate the palette gallery galleryControl1.Gallery.ColumnCount = 6 SkinHelper.InitSkinPaletteGallery(galleryControl1)