使用propertyGridControl控件,在Data工具栏下,核心是利用反射技术
1、定义工具类
添加引用:using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace XtraVerticalGrid
{
[DefaultPropertyAttribute("SaveOnClose")]
public class AppSettings1
{
private bool saveOnClose = true;
private string greetingText = "欢迎使用应用程序!";
private int maxRepeatRate = 10;
private int itemsInMRU = 4;
private bool settingsChanged = false;
private string appVersion = "1.0";
private string DefaultFileName = "Name";
private Size windowSize = new Size(100, 100);
private Font windowFont = new Font("宋体", 9, FontStyle.Regular);
private Color toolbarColor = SystemColors.Control;
[CategoryAttribute("文档设置"),
DefaultValueAttribute(true)]
public bool SaveOnClose
{
get { return saveOnClose; }
set { saveOnClose = value; }
}
[CategoryAttribute("文档设置")]
public Size WindowSize
{
get { return windowSize; }
set { windowSize = value; }
}
[CategoryAttribute("文档设置")]
public Font WindowFont
{
get { return windowFont; }
set { windowFont = value; }
}
[CategoryAttribute("全局设置")]
public Color ToolbarColor
{
get { return toolbarColor; }
set { toolbarColor = value; }
}
[CategoryAttribute("全局设置"),
ReadOnlyAttribute(true),
DefaultValueAttribute("欢迎使用应用程序!")]
public string GreetingText
{
get { return greetingText; }
set { greetingText = value; }
}
[CategoryAttribute("全局设置"),
DefaultValueAttribute(4)]
public int ItemsInMRUList
{
get { return itemsInMRU; }
set { itemsInMRU = value; }
}
[DescriptionAttribute("以毫秒表示的文本重复率。"),
CategoryAttribute("全局设置"),
DefaultValueAttribute(10)]
public int MaxRepeatRate
{
get { return maxRepeatRate; }
set { maxRepeatRate = value; }
}
[BrowsableAttribute(false),
DefaultValueAttribute(false)]
public bool SettingsChanged
{
get { return settingsChanged; }
set { settingsChanged = value; }
}
[CategoryAttribute("版本"),
DefaultValueAttribute("1.0"),
ReadOnlyAttribute(true)]
public string AppVersion
{
get { return appVersion; }
set { appVersion = value; }
}
[CategoryAttribute("拼写检查选项"),
DefaultValueAttribute("DefaultFileName")]
public string SpellingOptions
{
get { return this.DefaultFileName; }
set { this.DefaultFileName = value; }
}
}
}
2、直接创建此类的对象,将窗体绑定上来
AppSettings1 appset = new AppSettings1();
this.propertyGridControl1.SelectedObject = appset;
截图:
例子2:创建下拉框
方法:利用TypeConverter方法,将自定义的配置下拉项,转换
1、配置主界面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace XtraVerticalGrid
{
public class AppSettings
{
private string extendDirection = "横向";
private string extendConfig = "横向";
private string dataGroup = "不分组";
private string dataCollection = "合计";
private string blankLine = "不补充";
[CategoryAttribute("扩展方向"),
DefaultValueAttribute("横向"),
TypeConverter(typeof(ExtendDirection))]
public string 扩展方向
{
get { return extendDirection; }
set { extendDirection = value; }
}
[CategoryAttribute("可扩展性"),
DefaultValueAttribute("横向"),
TypeConverter(typeof(ExtendConfig))]
public string 可扩展性
{
get { return extendConfig; }
set { extendConfig = value; }
}
[CategoryAttribute("数据分组设置"),
DefaultValueAttribute("不分组"),
TypeConverter(typeof(DataGroup))]
public string 分组
{
get { return dataGroup; }
set { dataGroup = value; }
}
[CategoryAttribute("数据分组设置"),
DefaultValueAttribute("合计"),
TypeConverter(typeof(DataCollection))]
public string 汇总
{
get { return dataCollection; }
set { dataCollection = value; }
}
[CategoryAttribute("补充空白行"),
DefaultValueAttribute("不补充"),
TypeConverter(typeof(BlankLine))]
public string 补充空白行
{
get { return blankLine; }
set { blankLine = value; }
}
}
}
2、配置子选项内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace XtraVerticalGrid
{
/// <summary>
/// 扩展方向配置
/// </summary>
public class ExtendDirection : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "横向", "纵向"});
}
}
/// <summary>
/// 可扩展性配置
/// </summary>
public class ExtendConfig : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "横向", "纵向" });
}
}
/// <summary>
/// 数据分组设置
/// </summary>
public class DataGroup : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "不分组", "相同分组" });
}
}
/// <summary>
/// 数据汇总设置
/// </summary>
public class DataCollection : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "合计", "平均","最大","最小","记录数" });
}
}
/// <summary>
/// 补充空白行
/// </summary>
public class BlankLine : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "不补充", "补满页","固定行" });
}
}
}
例子3:配置子项的列表
1、配置主界面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace XtraVerticalGrid
{
[DefaultPropertyAttribute("SaveOnClose")]
public class AppSettings1
{
private bool saveOnClose = true;
private string greetingText = "欢迎使用应用程序!";
private int maxRepeatRate = 10;
private int itemsInMRU = 4;
private bool settingsChanged = false;
private string appVersion = "1.0";
<span style="color:#ff0000;">private SpellingOptions DefaultFileName = new SpellingOptions();</span>
private Size windowSize = new Size(100, 100);
private Font windowFont = new Font("宋体", 9, FontStyle.Regular);
private Color toolbarColor = SystemColors.Control;
[CategoryAttribute("文档设置"),
DefaultValueAttribute(true)]
public bool SaveOnClose
{
get { return saveOnClose; }
set { saveOnClose = value; }
}
[CategoryAttribute("文档设置")]
public Size WindowSize
{
get { return windowSize; }
set { windowSize = value; }
}
[CategoryAttribute("文档设置")]
public Font WindowFont
{
get { return windowFont; }
set { windowFont = value; }
}
[CategoryAttribute("全局设置")]
public Color ToolbarColor
{
get { return toolbarColor; }
set { toolbarColor = value; }
}
[CategoryAttribute("全局设置"),
ReadOnlyAttribute(true),
DefaultValueAttribute("欢迎使用应用程序!")]
public string GreetingText
{
get { return greetingText; }
set { greetingText = value; }
}
[CategoryAttribute("全局设置"),
DefaultValueAttribute(4)]
public int ItemsInMRUList
{
get { return itemsInMRU; }
set { itemsInMRU = value; }
}
[DescriptionAttribute("以毫秒表示的文本重复率。"),
CategoryAttribute("全局设置"),
DefaultValueAttribute(10)]
public int MaxRepeatRate
{
get { return maxRepeatRate; }
set { maxRepeatRate = value; }
}
[BrowsableAttribute(false),
DefaultValueAttribute(false)]
public bool SettingsChanged
{
get { return settingsChanged; }
set { settingsChanged = value; }
}
[CategoryAttribute("版本"),
DefaultValueAttribute("1.0"),
ReadOnlyAttribute(true)]
public string AppVersion
{
get { return appVersion; }
set { appVersion = value; }
}
[CategoryAttribute("拼写检查选项"),
TypeConverter(typeof(SpellingOptionsConverter)),
DefaultValueAttribute("DefaultFileName")]
public SpellingOptions SpellingOptions
{
get { return this.DefaultFileName; }
set { this.DefaultFileName = value; }
}
}
}
2、创建主界面下拉的子类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace XtraVerticalGrid
{
[DescriptionAttribute("展开以查看应用程序的拼写选项。")]
public class SpellingOptions
{
private bool spellCheckWhileTyping = true;
private bool spellCheckCAPS = false;
private bool suggestCorrections = true;
[DefaultValueAttribute(true)]
public bool SpellCheckWhileTyping
{
get { return spellCheckWhileTyping; }
set { spellCheckWhileTyping = value; }
}
[DefaultValueAttribute(false)]
public bool SpellCheckCAPS
{
get { return spellCheckCAPS; }
set { spellCheckCAPS = value; }
}
[DefaultValueAttribute(true)]
public bool SuggestCorrections
{
get { return suggestCorrections; }
set { suggestCorrections = value; }
}
}
}
3、创建类,判断类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
namespace XtraVerticalGrid
{
public class SpellingOptionsConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
System.Type destinationType)
{
if (destinationType == typeof(SpellingOptions))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, System.Type destinationType)
{
if (destinationType == typeof(System.String) &&
value is SpellingOptions)
{
SpellingOptions so = (SpellingOptions)value;
return "在键入时检查:" + so.SpellCheckWhileTyping +
",检查大小写: " + so.SpellCheckCAPS +
",建议更正: " + so.SuggestCorrections;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
try
{
string s = (string)value;
int colon = s.IndexOf(':');
int comma = s.IndexOf(',');
if (colon != -1 && comma != -1)
{
string checkWhileTyping = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string checkCaps = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
string suggCorr = s.Substring(colon + 1);
SpellingOptions so = new SpellingOptions();
so.SpellCheckWhileTyping = Boolean.Parse(checkWhileTyping);
so.SpellCheckCAPS = Boolean.Parse(checkCaps);
so.SuggestCorrections = Boolean.Parse(suggCorr);
return so;
}
}
catch
{
throw new ArgumentException(
"无法将“" + (string)value +
"”转换为 SpellingOptions 类型");
}
}
return base.ConvertFrom(context, culture, value);
}
}
}
截图: