两点C#的propertyGrid的使用心得【转】
源文:http://www.cnblogs.com/bicker/p/3318934.html
最近接触C#的PropertyGrid比较多,得到了两个小心得记录一下。
第1点是关于控制PropertyGrid中属性的只读属性的。
我遇到的问题是这样的,我需要在运行时根据SVN的状态动态控制PropertyGrid中的属性的读写控制。以前的做法比较简单,直接是 PropertyGrid.Enabled(false)。这样的坏处是完全使Grid完全失效,连滚动条也不可用了,不便于查看属性。后来上网查阅相关 的资料,网上有比较的是同一篇文章的复制,原文出处我已经找不到了。先把原文贴出来如下:
大家知道在类的某个属性中加[ReadOnlyAttribute(true)]声明标记后,此类的对象的这个属性在PropertyGrid中就表现为灰色不可更改,请问大家有没有什么办法动态地让这个属性在PropertyGrid中的显示变为可读写么?
以下的方法试过,不好用
1、想在程序里改声明标记,可是不行
2、另外写个类,同样的属性标记为[ReadOnlyAttribute(false)],然后重新selectobject,可是太复杂了。用反射可以实现动态改变,只读、可见等等,这些属性都可以改变。
以下两个方法分别实现可见性和只读属性的动态改变:
void SetPropertyVisibility(object obj, string propertyName, bool visible)
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(attrs[type], visible);
}void SetPropertyReadOnly(object obj, string propertyName, bool readOnly)
{
Type type = typeof(System.ComponentModel.ReadOnlyAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("isReadOnly",
BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
}使用时,SetPropertyVisibility(obj, "名称", true);
obj指的就是你的SelectObject, “名称”是你SelectObject的一个属性
当然,调用这两个方法后,重新SelectObject一下,就可以了心得:
(1)如果对属性框中所有属性一起进行控制,可以不添加 [ReadOnlyAttribute(false)]标记,propertyName可以是任何属性名称。[注:这里讲得很不清楚]
(2)如果仅仅对某一个属性进行控制,则必须在每个属性的描述中添加 [ReadOnlyAttribute(false)]标记。propertyName必须是所要控制的属性名。
原文中提到的思路是在运行时,通过反射的方式修改每一个Property的ReadOnlyAttribute。只是心得那里説不很不清楚,要对整个对象而非具体的属性进行控制时怎么办。
我的第一个想法是遍历所有的Property,对每一个都设置ReadOnly,但是这样是错误的,而且有副作用。后来经过试验,我直接对PropertyGrid的Object设置ReadOnly。

1 private void button1_Click(object sender, EventArgs e)
2 {
3 Type readonlyType = typeof(System.ComponentModel.ReadOnlyAttribute);
4 PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject);
5 FieldInfo fld = readonlyType.GetField("isReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.CreateInstance);
6 AttributeCollection attrs = TypeDescriptor.GetAttributes(propertyGrid1.SelectedObject);
7 fld.SetValue(attrs[typeof(ReadOnlyAttribute)], gridReadOnly);
8 gridReadOnly = !gridReadOnly;
9 }

这里説一下,在找解决办法的时候,还去顺便了解了一下c#在运行时,动态添加Attribute的内容,这个内容留下次再记录好了。
还找到一篇讲反射可以通过FieldInfo.SetValue设置任何字段的值的文章:http://www.cnblogs.com/Laser_Lu/archive/2004/08/01/29171.html
第2点是PropertyGrid中使用TypeConverter
PropertyGrid中对于自定义的类型显示支持有限,最好是自己去实现自己的TypeConverter,把类型转换来进行显示。我写了一个简单的例子,把List类型转换成string。

1 public class MyColorConverter : TypeConverter
2 {
3 public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
4 {
5 if (value == null)
6 {
7 return new List<int>();
8 }
9 string stringValue = value as string;
10 if (stringValue != null)
11 {
12 List<int> result = new List<int>();
13 string[] vs = stringValue.Split(new char[] { ',' });
14 foreach (string eachString in vs)
15 {
16 result.Add(int.Parse(eachString));
17 }
18 return result;
19 }
20 else
21 {
22 return base.ConvertFrom(context, culture, value);
23 }
24 }
25
26 public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
27 {
28 if (destinationType == typeof(string))
29 {
30 return true;
31 }
32 return base.CanConvertTo(context, destinationType);
33 }
34
35 public override System.ComponentModel.PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, System.Attribute[] attributes)
36 {
37 throw new Exception("The method or operation is not implemented.");
38 }
39
40 public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
41 {
42 if (destinationType == typeof(string))
43 {
44 List<int> list = value as List<int>;
45 if (list != null && list.Count > 0)
46 {
47 StringBuilder sb = new StringBuilder();
48 foreach (int v in list)
49 {
50 sb.AppendFormat("{0},", v);
51 }
52 sb.Remove(sb.Length - 1, 1);
53 return sb.ToString();
54 }
55 return "";
56 }
57 else
58 {
59 return base.ConvertTo(context, culture, value, destinationType);
60 }
61 }
62
63 public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
64 {
65 if (sourceType == typeof(string))
66 {
67 return true;
68 }
69 return base.CanConvertFrom(context, sourceType);
70 }
71 }


private List<int> color1 = new List<int>();
[Category("main")]
[DisplayName("颜色1")]
[TypeConverter(typeof(MyColorConverter))]
public List<int> Color1
{
get { return color1; }
set { color1 = value; }
}

ConvertFrom函数会在PropertyGrid中的字符串被修改保存后被调用
ConvertTo函数则是在最初显示PropertyGrid以及对List进行修改之后被调用
两点C#的propertyGrid的使用心得【转】的更多相关文章
- 两点C#的propertyGrid的使用心得
最近接触C#的PropertyGrid比较多,得到了两个小心得记录一下. 第1点是关于控制PropertyGrid中属性的只读属性的. 我遇到的问题是这样的,我需要在运行时根据SVN的状态动态控制Pr ...
- C# PropertyGrid控件应用心得
何处使用 PropertyGrid 控件 在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验.例如,某个应用程序包含多个用户可以设置的“设置”或选项 ...
- C# PropertyGrid控件应用心得 【转】
源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...
- 关于CSS中float的两点心得以及清除浮动的总结
对一个元素运用float后,该元素将脱离正常文档流,这意味着: 1. 运用float后,该元素不再影响父元素的高度,如果一个元素的所有子元素都是float的话,那么该元素的高度是0,这样后面元素渲染的 ...
- PropertyGrid控件由浅入深(二):基础用法
目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...
- windows类书的学习心得(转载)
原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...
- 百度api使用心得体会
最近项目中在使用百度地图api,对于其中的一些有用的点做一些归纳整理,如有不对的地方,欢迎各位大神纠正指出. 一定要学会查找百度地图api提供的类参考网站:http://lbsyun.baidu.co ...
- System.Windows.Forms.PropertyGrid的使用
PropertyGrid 控件简介 .NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心.PropertyGrid 控件显示对象或类型的属性,并主要 ...
随机推荐
- [转] 重定向 CORS 跨域请求
非简单请求不可重定向,包括第一个preflight请求和第二个真正的请求都不行. 简单请求可以重定向任意多次,但如需兼容多数浏览器,只可进行一次重定向. 中间服务器应当同样配置相关 CORS 响应头. ...
- [每日App一]QQ主题要逆天!轻轻松松月入30万!
听从吾师秦刚(微信或QQ:1111884)酋长的建议,谋哥(微信viyi88)我开始新的征程,每日更新一篇干货文章(要坚持啊!否则被酋长鄙视了). 好了,废话不多说,今天我给大家揭秘一个你从来想也木有 ...
- 大数据学习——scala集合练习
package com /** * Created by ZX on 2016/4/5. */ object ListTest { def main(args: Array[String]) { // ...
- python - web自动化测试 - 文件上传操作
# 12. 上传操作## (1)如果是input可以直接输入路径的,直接使用send_keys输入路径# (2)非input标签的,需要借助第三方工具:# A. AutoIt : 调用其生成的au3或 ...
- WordPress后台添加侧边栏菜单
add_action('admin_menu', 'register_custom_menu_page'); function register_custom_menu_page() { add_me ...
- Educational Codeforces Round 20 A. Maximal Binary Matrix
A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 慢查询阻塞了xtrabackup进而阻塞以后的sql导致的系统瘫痪问题
收到开发反应一库的sql频繁超时,系统几乎瘫痪,无法执行任何操作,我登上库先查看到当前的线程,发现有大量的线程状态是 Waiting for table flush 查看当前的事务 从昨天开始执行,到 ...
- redhat linux 从/home目录扩展空间至/根目录
查看分区大小 [root@easdb01 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/vg_easdb01-lv_ro ...
- iOS-----5分钟学会枚举的正确使用姿势-Enumeration宏
前言 Enum,枚举,相信大部分编程语言都有对应的枚举类型,功能可能有多有少,但是枚举最核心的功能是 “规范的定义代码中的状态.状态码.选项”. 状态.状态码.选项 什么是状态:同时只能出现一个值(状 ...
- 漫谈DNS
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6189633.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...