构建自定义最终用户皮肤选择器
本节介绍如何用DevExpress皮肤项填充ComboBoxEdit控件。
1.要填充组合框编辑器,迭代集合(它返回所有当前可用的DevExpress皮肤),并为每个适用的皮肤创建一个新的组合框项。如下面的代码所示:
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
您可以实现更复杂的逻辑来跳过或重命名特定的皮肤。例如,下面的代码示例排除了“Caramel”皮肤和任何“Office 2007…”皮肤,并将“DevExpress Style”和“DevExpress Dark Style”皮肤分别重命名为“Default skin”和“Default Dark skin”。
C#:
DevExpress.UserSkins.BonusSkins.Register(); foreach (SkinContainer cnt in SkinM说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.处理事件,以便在用户选择组合框项时应用相应的皮肤。
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.运行应用程序来查看结果。
SkinHelper方法
DevExpress.XtraBars.Helper.SkinHelper类提供了多个Init…方法,这些方法允许您使用皮肤/调色板项填充自定义控件。
在下面的图中,两个功能区按钮是自定义皮肤和调色板选择器,两个按钮的BarButtonItemButtonStyle属性都等于DropDown。每个按钮都有一个关联的PopupControlContainer分配给BarButtonItemDropDownControl属性,弹出控件容器承载的库控件的Dock属性设置为“填充”。
下面的代码演示了如何使用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)