一、自定义一个特性类 ListAttribute ,提供下拉列表值:
using System;
using System.Collections.Generic;
using System.Text; using System.Collections;
namespace PropertyGrid {
class ListAttribute : Attribute {
public string[] lists;
public ListAttribute() {
lists = new string[] { "A", "B", "C", "D", "E" };//如果要实现动态下拉列表,在此处初始化lists对象值
}
}
}
二、一时想不起来了,就叫它特性转换器MyConverter
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace PropertyGrid {
class MyConverter : ExpandableObjectConverter {
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
ListAttribute lst = (ListAttribute)context.PropertyDescriptor.Attributes[typeof(ListAttribute)]; StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(lst.lists);
return vals;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
return true;
}
}
}
三、应用示例:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace PropertyGrid {
class MyObject {
private int id;
public int ID {
get {
return id;
}
set {
id = value;
}
}
private string code;
public string Code {
get {
return code;
}
set {
code = value;
}
}
private string name;
[CategoryAttribute("信息"),DescriptionAttribute("名称"),TypeConverter(typeof(MyConverter)),ListAttribute()] public string Name {
get {
return name;
}
set {
name = value;
}
}
public MyObject() {
this.id = -1;
this.code = "";
this.name = "";
}
}
}
这句你不会忘记吧:propertyGrid1.SelectedObject = new MyObject();
Ok,搞定!
分享到:
相关推荐
.net C# PropertyGrid 显示下拉列表
PropertyGrid 下拉式和弹出编辑框的实现
工作中用到的 Winform 属性编辑器 propertyGrid示例 下拉列表框
源代码,含例程。 加上代码中特性,就可以显示成中文,支持下拉菜单选择文本
c# PropertyGrid中复杂类型自定义显示(装)自用
手动动态添加 PropertyGrid 的数据行并显示 C# (非属性绑定方式)
C# 自定义属性在propertyGrid控件中显示 演示demo
在网上找了一些PropertyGrid的文章,也有可以实现多语言版本的,但是那种方法使PropertyGrid无法传值,没有实际意义。这个demo可以将CategoryName、PropertyName、PropertyDescription设置多国语言。并且不会出现...
c#winfrom PropertyGrid 运行时变更属性项 关于PropertyGrid相关参见:http://www.cnblogs.com/mywebname/archive/2007/11/15/959732.html
自己编写的 主要想自己留着备份的 带有绑定控件 分层 类目。亲们喜欢也可以下载参考参考
PropertyGrid 联动 自定义下拉框 实现 PropertyGrid 二级联动
在C#2019中,将XML结点动态映射为属性集,然后使用PropertyGrid加载并显示。支持父结点的名称自动映射为Category(目录).
一个基于C# +VS2008实现的PropertyGrid高级扩展控件源码。
关于propertyGrid加载自定义属性的小Demo,可以根据自己需要进行更改
PropertyGrid控件使用教程 PropertyGrid控件使用教程
利用微软的propertygrid快捷的实现基于attribute的开发。
使用C#自定义propertygrid,能够自定义属性和显示顺序
一个关于C# PropertyGrid增加自定义属性的范例。
在winform开发中,利用PropertyGrid控件来显示、编辑对象的属性是非常方便的,本例程以一个公司类为例来介绍在Visual Studio中用PropertyGrid显示和编辑集合的属性,其中,公司对象中包含员工对象的集合。