C# DevExpress WinForm编程 针对一些控件重复度高的属性设置问题的解决方案
2017-12-26 10:33
441 查看
问题描述
我在使用DevExpress控件时经常会需要先将控件设置成自己需要的样式或者取消控件的一部分功能(可删除、可拖曳之类的)。这种行为本身并没什么问题,问题在于DevExpress控件的属性实在是太多了,多到我每次打开属性界面都会头痛。解决方案
为了解决这个问题我使用了工具类提取控件属性的方法。如下图,我将XtraForm传入了ZXtraFormTools这个工具类,也将菜单Bar对象传入了ZXtraBarTools工具类进行常用属性的设置。
这两个工具类的属性视图只保留了我要用的属性。
能看到这里的大家应该都懂了,下面直接放代码。
ZXtraFormTools.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using DevExpress.XtraEditors; namespace ZDevExpressTools { public class ZXtraFormTools : ZComponentTools<XtraForm> { [Browsable(true), DefaultValue(null), DXCategory("对象引用")] public XtraForm XtraForm { get { return this.OperationObject; } set { this.OperationObject = value; } } [Browsable(true), DXCategory("视图选项")] public bool ShowIcon { get { if (this.XtraForm != null) { return this.XtraForm.ShowIcon; } return false; } set { if (this.XtraForm != null) { this.XtraForm.ShowIcon = value; } } } [AmbientValue("")] [Localizable(true)] [Browsable(true), DXCategory("视图选项")] public Icon Icon { get { if (this.XtraForm != null) { return this.XtraForm.Icon; } return null; } set { if (this.XtraForm != null) { this.XtraForm.Icon = value; } } } [Browsable(true), DXCategory("视图选项")] public string Text { get { if (this.XtraForm != null) { return this.XtraForm.Text; } return null; } set { if (this.XtraForm != null) { this.XtraForm.Text = value; } } } [Category("常用属性")] public bool ShowInTaskbar { get { if (this.XtraForm != null) { return this.XtraForm.ShowInTaskbar; } return false; } set { if (this.XtraForm != null) { this.XtraForm.ShowInTaskbar = value; } } } [Category("常用属性")] public bool MaximizeBox { get { if (this.XtraForm != null) { return this.XtraForm.MaximizeBox; } return false; } set { if (this.XtraForm != null) { this.XtraForm.MaximizeBox = value; } } } [Category("常用属性")] public bool MinimizeBox { get { if (this.XtraForm != null) { return this.XtraForm.MinimizeBox; } return false; } set { if (this.XtraForm != null) { this.XtraForm.MinimizeBox = value; } } } [Localizable(true)] [Category("常用属性")] public FormStartPosition StartPosition { get { if (this.XtraForm != null) { return this.XtraForm.StartPosition; } return FormStartPosition.WindowsDefault 10897 Location; } set { if (this.XtraForm != null) { this.XtraForm.StartPosition = value; } } } [DispId(-504)] [Category("常用属性")] public FormBorderStyle FormBorderStyle { get { if (this.XtraForm != null) { return this.XtraForm.FormBorderStyle; } return System.Windows.Forms.FormBorderStyle.None; } set { if (this.XtraForm != null) { this.XtraForm.FormBorderStyle = value; } } } [Category("常用属性")] public bool TopMast { get { if (this.XtraForm != null) { return this.XtraForm.TopMost; } return false; } set { if (this.XtraForm != null) { this.XtraForm.TopMost = value; } } } } }
ZXtraBarTools.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Drawing; using DevExpress.XtraEditors; using DevExpress.XtraBars; using DevExpress.Utils; using DevExpress.Utils.Design; using DevExpress.Utils.Serializing; namespace ZDevExpressTools { public class ZXtraBarTools : Component { private Bar bar; [Browsable(true), Category("对象引用")] public Bar Bar { get { return bar; } set { bar = value; } } #region 常用属性 [Browsable(true), Category("常用属性")] public bool AllowDelete { get { if (Bar != null) { return Bar.OptionsBar.AllowDelete; } else { return false; } } set { if (Bar != null) { Bar.OptionsBar.AllowDelete = value; } } } [Browsable(true), Category("常用属性")] public bool AllowQuickCustomization { get { if (Bar != null) { return Bar.OptionsBar.AllowQuickCustomization; } else { return false; } } set { if (Bar != null) { Bar.OptionsBar.AllowQuickCustomization = value; } } } [Browsable(true), Category("常用属性")] public bool DrawDragBorder { get { if (Bar != null) { return Bar.OptionsBar.DrawDragBorder; } else { return false; } } set { if (Bar != null) { Bar.OptionsBar.DrawDragBorder = value; } } } [Browsable(true), Category("常用属性")] public bool MultiLine { get { if (Bar != null) { return Bar.OptionsBar.MultiLine; } else { return false; } } set { if (Bar != null) { Bar.OptionsBar.MultiLine = value; } } } [Browsable(true), Category("常用属性")] public bool UseWholeRow { get { if (Bar != null) { return Bar.OptionsBar.UseWholeRow; } else { return false; } } set { if (Bar != null) { Bar.OptionsBar.UseWholeRow = value; } } } #endregion [DXCategory("Font")] [DXDisplayName(typeof(ResFinder), "PropertyNamesRes", "DevExpress.Utils.AppearanceObject.Font")] [Localizable(true)] [RefreshProperties(RefreshProperties.All)] [TypeConverter(typeof(FontTypeConverter))] [XtraSerializableProperty] public Font Font { get { if (Bar != null) { return Bar.BarAppearance.Normal.Font; } else { return null; } } set { if (Bar != null) { Bar.BarAppearance.Normal.Font = value; Bar.BarAppearance.Pressed.Font = value; Bar.BarAppearance.Hovered.Font = value; Bar.BarAppearance.Disabled.Font = value; } else { } } } } }
可能会碰到的问题
这个方案其实有一个问题,就是Winform的对象初始化顺序(上例不会碰到这个问题,因为控件拖放在工具类之前)。就算在设计视图中看到的是正常的,实际运行的时候可能是另一个样子。针对这个问题我的解决方案是当工具类实例加载对象时重载设置。代码有点复杂,包含了对控件属性的加载、重置以及复用。实际就是用Attribute标识需要重载的属性,再用反射机制赋值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Reflection; using ZTools; namespace ZDevExpressTools { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> public abstract class ZComponentTools<T> : Component where T : Component { private bool allowReuse = false; /// <summary> /// 允许复用 /// </summary> [Browsable(true), DefaultValue(false), Category("复用设置")] public bool AllowReuse { get { return allowReuse; } set { allowReuse = value; } } private T operationObject; [Category("对象引用")] /// <summary> /// 操作对象 /// </summary> public T OperationObject { get { return this.operationObject; } set { if (this.operationObject == null) { //LoadSettings //若原来的操作对象为空则加载属性 this.LoadSettings(value); } else { if (this.AllowReuse == true) { //ReuseSettings //若原来的操作对象不为空 且 Tools需要复用 则 复用属性 this.ReuseSettings(value); } else { //ResetSettings //若原来的操作对象不为空 且 Tools不需要复用 则 重设属性 this.ResetSettings(); } } this.operationObject = value; } } /// <summary> /// 复用Tools的设置 /// </summary> /// <param name="obj"></param> public void ReuseSettings(T obj) { //1.取出对象中包含指令特性的成员属性 List<PropertyInfo> list = ZAttrGetter.GetPropertyInfosConformAttribute(this, typeof(ZComponentToolsSettingsAttribute)); List<PropertyInfo> piList = new List<PropertyInfo>(); List<object> temp = new List<object>(); //2.遍历成员属性信息表判断是否复用,添加到属性值缓存表 foreach (PropertyInfo pi in list) { ZComponentToolsSettingsAttribute attr = ZAttrGetter.GetPropertyAttr(pi, typeof(ZComponentToolsSettingsAttribute)) as ZComponentToolsSettingsAttribute; if (attr.Reuse == true) { piList.Add(pi); temp.Add(pi.GetValue(this)); } } //3.将 属性值缓存表中的值 重新 赋给 Tools 的 成员属性 this.operationObject = obj; for (int i=0; i<temp.Count; i++) { try { piList[i].SetValue(this, temp[i]); } catch (Exception) { } } } /// <summary> /// 重设Tools的设置 /// </summary> public void ResetSettings() { this.operationObject = null; //1.取出对象中包含指令特性的成员属性 List<PropertyInfo> list = ZAttrGetter.GetPropertyInfosConformAttribute(this, typeof(ZComponentToolsSettingsAttribute)); List<PropertyInfo> piList = new List<PropertyInfo>(); List<object> temp = new List<object>(); //2.遍历成员属性信息表判断是否复用,添加到属性值缓存表 foreach (PropertyInfo pi in list) { ZComponentToolsSettingsAttribute attr1 = ZAttrGetter.GetPropertyAttr(pi, typeof(ZComponentToolsSettingsAttribute)) as ZComponentToolsSettingsAttribute; DefaultValueAttribute attr2 = ZAttrGetter.GetPropertyAttr(pi, typeof(DefaultValueAttribute)) as DefaultValueAttribute; if (attr1.Reset == true) { if (attr2 != null) { piList.Add(pi); temp.Add(attr2.Value); } } } //3.将 属性值缓存表中的值 重新 赋给 Tools 的 成员属性 for (int i = 0; i < temp.Count; i++) { try { piList[i].SetValue(this, temp[i]); } catch (Exception) { } } } /// <summary> /// 加载Tools的设置 /// </summary> /// <param name="obj"></param> public void LoadSettings(T obj) { //1.取出对象中包含指令特性的成员属性 List<PropertyInfo> list = ZAttrGetter.GetPropertyInfosConformAttribute(this, typeof(ZComponentToolsSettingsAttribute)); List<PropertyInfo> piList = new List<PropertyInfo>(); List<object> temp = new List<object>(); //2.遍历成员属性信息表判断是否加载,添加到属性值缓存表 foreach (PropertyInfo pi in list) { ZComponentToolsSettingsAttribute attr = ZAttrGetter.GetPropertyAttr(pi, typeof(ZComponentToolsSettingsAttribute)) as ZComponentToolsSettingsAttribute; if (attr.Load == true) { piList.Add(pi); temp.Add(pi.GetValue(this)); } } //3.将 属性值缓存表中的值 重新 赋给 Tools 的 成员属性 this.operationObject = obj; for (int i = 0; i < temp.Count; i++) { try { piList[i].SetValue(this, temp[i]); } catch (Exception) { } } } } /// <summary> /// /// </summary> [AttributeUsage(AttributeTargets.All)] public class ZComponentToolsSettingsAttribute : Attribute { private bool load = false; private bool reuse = false; private bool reset = false; public ZComponentToolsSettingsAttribute(bool Load = false, bool Reuse = false, bool Reset = true) { this.load = Load; this.reuse = Reuse; this.reset = Reset; } /// <summary> /// 允许加载 /// </summary> public bool Load { get { return this.load; } set { this.load = value; } } /// <summary> /// 复用 /// </summary> public bool Reuse { get { return this.reuse; } set { this.reuse = value; } } /// <summary> /// 重设 /// </summary> public bool Reset { get { return this.reset; } set { this.reset = value; } } } }
相关文章推荐
- DevExpress控件 在 winform 上的一些控件的常用属性设置
- WinForm开发 DataGridView控件的各种操作总结(三、针对datagridview全局属性的设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- 【转】c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- DevExpress中关于GridControl控件的一些常用属性设置方法
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题解决方案
- C# 中winform的一些属性设置
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- C#一些控件的属性设置和用处
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- C# WinForm控件开发如何设置属性的默认值
- 解决WinForm中ComboBox控件的“设置"DataSourse”属性后无法修改项集合”以及两个不相关联的ComboxBox控件实现数据列表显示不可实现的问题
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值?
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)