c#propertygrid1 是否有内容

自定义属性在propertyGrid控件中显示
在上篇文章(地址:)中可以看到:
自定义属性的显示是有问题的,那么如何修改呢?
代码如下:
public class PropertyDisplayConverterr&T& : ExpandableObjectConverter where T : IDisplay
public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
if (destinationType == typeof(T))
return base.CanConvertTo(context, destinationType);
// This is a special type converter which will be associated with the T class.
// It converts an T object to string representation for use in a property grid.
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
if (destinationType == typeof(System.String) && value is T)
return ((IDisplay)value).GetDisplayString();
return base.ConvertTo(context, culture, value, destinationType);
public interface IDisplay
/// &summary&
/// 得到显示字符串
/// &/summary&
/// &returns&&/returns&
string GetDisplayString();
}修改上文中实体类如下:
[TypeConverterAttribute(typeof(PropertyDisplayConverterr&IdentityColumnEntity&))]
public class IdentityColumnEntity : IDisplay
private bool isIncrementC
/// &summary&
/// 是否是自增列
/// &/summary&
[Browsable(true)]
[Category("基本")]
[DisplayName("是否是自增列")]
[ReadOnly(false)]
[DefaultValue(false)]
public bool IsIncrementColumn
set { isIncrementColumn = }
get { return isIncrementC }
private Int64 identityI
/// &summary&
/// 标识增量
/// &/summary&
[Browsable(true)]
[Category("基本")]
[DisplayName("标识增量")]
[ReadOnly(false)]
[Description("标识增量属性指定在 Microsoft SQL Server 为插入的行生成标识值时,在现有的最大行标识值基础上所加的值。标识增量必须是 非零 整数,位数等于或小于 10。")]
public Int64 IdentityIncrement
set { identityIncrement = }
get { return identityI }
private Int64 ident_S
/// &summary&
/// 标识种子
/// &/summary&
[Browsable(true)]
[Category("基本")]
[DisplayName("标识种子")]
[ReadOnly(false)]
[Description("指示标识列的初始行值。标识种子必须是
整数,位数等于或小于 10。")]
public Int64 Ident_Seed
set { ident_Seed = }
get { return ident_S }
public string GetDisplayString()
if (this == null || this.IdentityIncrement == 0)
return "未设置自增列信息";
return String.Format("标识种子:{0};标识增量:{1}", this.Ident_Seed, this.IdentityIncrement);
}效果如下:
演示代码:
本文参考:
参考文章中demo:本帖子已过去太久远了,不再提供回复功能。&&&您需要以后才能回答,未注册用户请先。C#实现ProperTyGrid自定义属性的方法
投稿:shichen2014
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了C#实现ProperTyGrid自定义属性的方法,主要通过接口ICustomTypeDescriptor实现,需要的朋友可以参考下
本文实例讲解了C#实现ProperTyGrid自定义属性的方法,分享给大家供大家参考。具体方法如下:
一般来说,C#如果要实现自定义属性必须要需要实现接口ICustomTypeDescriptor,具体实现方法如下:
// 提供为对象提供动态自定义类型信息的接口。
public interface ICustomTypeDescriptor
/// &summary&
/// 自定义属性对象
/// &/summary&
public class MyAttr
private string name = string.E
public string Name
set { name = }
private object value =
public object Value
get { return this. }
set { this.value = }
private string description = string.E
public string Description
set { description = }
public override string ToString()
return string.Format("Name:{0},Value:{1}",name.ToString(),value.ToString());
/// &summary&
/// 自定义性质描述类
/// &/summary&
public class MyPropertyDescription : PropertyDescriptor
private MyAttr myattr =
public MyPropertyDescription(MyAttr myattr, Attribute[] attrs): base(myattr.Name, attrs)
this.myattr =
public override bool CanResetValue(object component)
public override Type ComponentType
return this.GetType();
public override object GetValue(object component)
return myattr.V
public override bool IsReadOnly
public override Type PropertyType
return myattr.Value.GetType();
public override void ResetValue(object component)
//不重置,无动作
public override void SetValue(object component, object value)
myattr.Value =
/// &summary&
/// 是否应该持久化保存
/// &/summary&
/// &param name="component"&&/param&
/// &returns&&/returns&
public override bool ShouldSerializeValue(object component)
/// &summary&
/// 属性说明
/// &/summary&
public override string Description
return myattr.D
/// &summary&
/// 实现自定义的特殊属性对象必须继承ICustomTypeDescriptor,并实现Dictionary
/// &/summary&
public class MyAttrCollection : Dictionary&String, MyAttr&, ICustomTypeDescriptor
/// &summary&
/// 重写Add方法
/// &/summary&
/// &param name="attr"&&/param&
public void Add(MyAttr attr)
if (!this.ContainsKey(attr.Name))
base.Add(attr.Name, attr);
public AttributeCollection GetAttributes()
return TypeDescriptor.GetAttributes(this, true);
public string GetClassName()
return TypeDescriptor.GetClassName(this,true);
public string GetComponentName()
return TypeDescriptor.GetClassName(this, true);
public TypeConverter GetConverter()
return TypeDescriptor.GetConverter(this, true);
public EventDescriptor GetDefaultEvent()
return TypeDescriptor.GetDefaultEvent(this, true);
public PropertyDescriptor GetDefaultProperty()
return TypeDescriptor.GetDefaultProperty(this, true);
public object GetEditor(Type editorBaseType)
return TypeDescriptor.GetEditor(this, editorBaseType, true);
public EventDescriptorCollection GetEvents(Attribute[] attributes)
return TypeDescriptor.GetEvents(this, attributes, true);
public EventDescriptorCollection GetEvents()
return TypeDescriptor.GetEvents(this, true);
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
int count=this.Values.C
PropertyDescriptor[] pds=new PropertyDescriptor[count];
int index = 0;
foreach (MyAttr item in this.Values)
pds[index] = new MyPropertyDescription(item,attributes);
return new PropertyDescriptorCollection(pds);
public PropertyDescriptorCollection GetProperties()
return TypeDescriptor.GetProperties(this,true);
public object GetPropertyOwner(PropertyDescriptor pd)
前台调用如下图所示:
private void btnAddProperType_Click(object sender, EventArgs e)
MyAttr attr = new MyAttr();
attr.Name = txtName.Text.Trim();
attr.Value = txtValue.Text.Trim();
attr.Description = txtDescription.Text.Trim();
mac.Add(attr);
MyGrid.Refresh();
private void button1_Click(object sender, EventArgs e)
AddAttrColor();
AddAttrImage();
AddAttrEmun();
MyGrid.Refresh();
private void AddAttrEmun()
MyAttr attr = new MyAttr();
attr.Name = "Dock";
attr.Value = DockStyle.F
attr.Description = "枚举";
mac.Add(attr);
private void AddAttrImage()
MyAttr attr = new MyAttr();
attr.Name = "Image";
attr.Value = new Bitmap(400,300);
attr.Description = "图片";
mac.Add(attr);
private void AddAttrColor()
MyAttr attr = new MyAttr();
attr.Name = "Color";
attr.Value = Color.R
attr.Description = "颜色";
mac.Add(attr);
运行效果如下图所示:
希望本文所述对大家的C#程序设计有所帮助
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具既然是一个窗体设计器,那就应该能够设置控件的属性,设置属性最好的当然是PropertyGrid了,我们仅仅需要使用一个PropertyGrid.SelectedObject = Control就可以搞定,让PropertyGrid显示Control的所有属性。可是这里显示的属性名是英文的。对于我们开发人员来说这无可厚非,我们也乐于接受。并且让PropertyGrid显示中文属性名,这对于我们开发人员的使用来说显得多此一举。可是,对于我这种类型的一个应用工具,英文属性名对于很多客户来说可能就很难懂了。所以应该让PrpertyGrid能够显示中文属性名。
另外,对于这样的一个系统。并不是控件的所有属性都是用户希望的,可能用户希望看到的仅仅是控件高度、控件宽度、控件文本。。等等的属性,但是如果直接将一个控件属性全部显示给用户的话,估计对用户造成的干扰和困惑是很大的。如何解决这个问题呢?其实用户控件开发的时候,如果我们不希望此属性在中显示,我们可以设置这个控件的,如:
[Browsable(false)]
public int Width
通过使用BrowsableAttribute就可以设置将此属性对隐藏。
你可能要问到了,对于控件来说,其中的很多属性都是直接继承来的,我们并没有办法控制是否对隐藏啊?呵呵,对啊,这就是我下面要说的解决方法(当然此方法显得不是很灵活,但是对于这种类型的系统的确相当有用)。
在我的解决方式中,我不直接这样,而是把这个替换成一个专门为此类型的设计的类对象上。比如我对设计了一个TextBoxProperty,这样我们使用的是TextBoxProperty的一个对象。
下面就是TextBoxProperty的代码:
public class TextBoxProperty : PropertyBase
private TextBox _C
public TextBoxProperty()
public TextBoxProperty(TextBox control)
this._Control =
[MyControlAttibute("文本", "获取或者设置控件文本", "")]
public string Text
get { return this._Control.T }
this._Control.Text =
[MyControlAttibute("宽度", "获取或者设置控件宽度", "")]
public int Width
get { return this._Control.W }
this._Control.Width = (int)
[MyControlAttibute("高度", "获取或者设置控件高度", "")]
public int Height
get { return this._Control.H }
this._Control.Height = (int)
[MyControlAttibute("上边距", "获取或者设置控件上边位置", "")]
public int Top
get { return this._Control.T }
this._Control.Top =
[MyControlAttibute("左边距", "获取或者设置控件左边位置", "")]
public int Left
get { return this._Control.L }
this._Control.Left =
[MyControlAttibute("背景色", "获取或者设置控件背景颜色", "")]
public Color BackColor
get { return this._Control.BackC }
this._Control.BackColor =
[MyControlAttibute("前景色", "获取或者设置控件的前景颜色", "")]
public Color ForeColor
get { return this._Control.ForeC }
this._Control.ForeColor =
你从代码里面已经看出了一些端倪了,在TextBoxProperty中每个要属性都增加了MyControlAttibute,具体的概念和使用方法您可以参考我的另一篇文《》。这里对做了详细的介绍(对初学者有效)。所以我就直接贴出MyControlAttibute的代码好了:
public class MyControlAttibute : Attribute
private string _PropertyN
private string _PropertyD
private object _DefaultV
public MyControlAttibute(string Name, string Description, object DefalutValue)
this._PropertyName = N
this._PropertyDescription = D
this._DefaultValue = DefalutV
public MyControlAttibute(string Name, string Description)
this._PropertyName = N
this._PropertyDescription = D
this._DefaultValue = "";
public MyControlAttibute(string Name)
this._PropertyName = N
this._PropertyDescription = "";
this._DefaultValue = "";
public string PropertyName
get { return this._PropertyN }
public string PropertyDescription
get { return this._PropertyD }
public object DefaultValue
get { return this._DefaultV }
通过上面的两段代码,你已经初步看出了在中显示中文属性以及仅仅显示我们需要的属性的基本思路。下面就介绍的就是怎么让MyControlAttibute中定义了的中文属性名在中显示出来。下面这段代码,我仅仅贡献了PropertyStub中这个函数的实现。所以不做过多解释了:
public override string DisplayName
if (info != null)
MyControlAttibute uicontrolattibute = (MyControlAttibute)Attribute.GetCustomAttribute(info, typeof(MyControlAttibute));
if (uicontrolattibute != null)
return uicontrolattibute.PropertyN
return info.N
return "";
整个代码如下,里面包含两个类,你直接拷贝出来就可以使用了:
public delegate void PropertyChanged(object Value);
/// &summary&
/// 主要是实现中文化属性显示
/// &/summary&
public class PropertyBase : ICustomTypeDescriptor
/// &summary&
/// 下面这段代码来源于:/Html/Csdn/2_47/View_4702219.html
/// &/summary&
/// &returns&&/returns&
#region ICustomTypeDescriptor 显式接口定义
AttributeCollection ICustomTypeDescriptor.GetAttributes()
return TypeDescriptor.GetAttributes(this, true);
string ICustomTypeDescriptor.GetClassName()
return TypeDescriptor.GetClassName(this, true);
string ICustomTypeDescriptor.GetComponentName()
return TypeDescriptor.GetComponentName(this, true);
TypeConverter ICustomTypeDescriptor.GetConverter()
return TypeDescriptor.GetConverter(this, true);
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
return TypeDescriptor.GetDefaultEvent(this, true);
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
return null;
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
return TypeDescriptor.GetEditor(this, editorBaseType, true);
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
return TypeDescriptor.GetEvents(this, true);
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
return TypeDescriptor.GetEvents(this, attributes, true);
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[<span style="color: #]);
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
ArrayList props = new ArrayList();
Type thisType = this.GetType();
PropertyInfo[] pis = thisType.GetProperties();
foreach (PropertyInfo p in pis)
if (p.DeclaringType == thisType || p.PropertyType.ToString() == "System.Drawing.Color")
//判断属性是否显示
BrowsableAttribute Browsable = (BrowsableAttribute)Attribute.GetCustomAttribute(p, typeof(BrowsableAttribute));
if (Browsable != null)
if (Browsable.Browsable == true || p.PropertyType.ToString() == "System.Drawing.Color")
PropertyStub psd = new PropertyStub(p, attributes);
props.Add(psd);
PropertyStub psd = new PropertyStub(p, attributes);
props.Add(psd);
PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(propArray);
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
return this;
#endregion
/// &summary&
/// 下面这段代码来源于:/Html/Csdn/2_47/View_4702219.html
/// &/summary&
public class PropertyStub : PropertyDescriptor
public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs)
: base(propertyInfo.Name, attrs)
this.info = propertyI
public override Type ComponentType
get { return this.info.ReflectedT }
public override bool IsReadOnly
get { return this.info.CanWrite == false; }
public override Type PropertyType
get { return this.info.PropertyT }
public override bool CanResetValue(object component)
return false;
public override object GetValue(object component)
//Console.WriteLine("GetValue: " + component.GetHashCode());
return this.info.GetValue(component, null);
return null;
public override void ResetValue(object component)
public override void SetValue(object component, object value)
//Console.WriteLine("SetValue: " + component.GetHashCode());
this.info.SetValue(component, value, null);
public override bool ShouldSerializeValue(object component)
return false;
//通过重载下面这个属性,可以将属性在PropertyGrid中的显示设置成中文
public override string DisplayName
if (info != null)
MyControlAttibute uicontrolattibute = (MyControlAttibute)Attribute.GetCustomAttribute(info, typeof(MyControlAttibute));
if (uicontrolattibute != null)
return uicontrolattibute.PropertyN
return info.N
return "";
修改后的界面如下:
所以我就是直接将创建对象的代码放在了Control_Click中:
//我这里仅仅做TextBox的属性演示,如果是其它的控件的话,那么你需要设计不同的ControlProperty(比如TextBoxProperty,ComboBoxProperty)
if (sender is TextBox)
this.propertyGrid1.SelectedObject = new TextBoxProperty((TextBox)sender);
this.propertyGrid1.SelectedObject = null;
对于实际的需要来说应该是根据Control的类型,通过工厂模式来创建ControlProperty。
Form的整个代码如下,窗体如上图所示:
//在Form中增加几个Button,分别命名为cmdArrow,cmdLabel,cmdTextBox,cmdComboBox,cmdGroupBox
public partial class Form1 : Form
private MouseHook _MouseH
//我们将所有的已经与具体控件关联了的UISizeKnob缓存在这个HashTable中
private Hashtable _HashUISizeK
//负责控件移动的类
private Hashtable _HashUIMoveK
public Form1()
InitializeComponent();
this._MouseHook = new MouseHook(this);
this._HashUISizeKnob = new Hashtable();
this._HashUIMoveKnob = new Hashtable();
//为了简洁明了,我们在ControlAdded中来设置具体控件和UISizeKnob的关联
this.ControlAdded += new ControlEventHandler(Form1_ControlAdded);
void Form1_ControlAdded(object sender, ControlEventArgs e)
if (!(e.Control is UISizeDot))
this._HashUISizeKnob.Add(e.Control, new UISizeKnob(e.Control));
this._HashUIMoveKnob.Add(e.Control, new UIMoveKnob(e.Control));
//点击控件的时候,显示控件的选择
e.Control.Click += new EventHandler(Control_Click);
void Control_Click(object sender, EventArgs e)
//寿险清除已经选择的控件
foreach (UISizeKnob knob in this._HashUISizeKnob.Values)
knob.ShowUISizeDots(false);
//ponentModel.Design.ISelectionService
//System.Drawing.Design.IToolboxService
((UISizeKnob)this._HashUISizeKnob[sender]).ShowUISizeDots(true);
//我这里仅仅做TextBox的属性演示,如果是其它的控件的话,那么你需要设计不同的ControlProperty(比如TextBoxProperty,ComboBoxProperty)
if (sender is TextBox)
this.propertyGrid1.SelectedObject = new TextBoxProperty((TextBox)sender);
this.propertyGrid1.SelectedObject = null;
private void cmdArrow_Click(object sender, EventArgs e)
SettingService.Instance.SelectedToolBoxControl = null;
private void cmdLabel_Click(object sender, EventArgs e)
SettingService.Instance.SelectedToolBoxControl = new Label();
private void cmdTextBox_Click(object sender, EventArgs e)
SettingService.Instance.SelectedToolBoxControl = new TextBox();
private void cmdComboBox_Click(object sender, EventArgs e)
SettingService.Instance.SelectedToolBoxControl = new ComboBox();
private void cmdGroupBox_Click(object sender, EventArgs e)
SettingService.Instance.SelectedToolBoxControl = new GroupBox();
好了,让显示中文属性的思路和代码都给了,希望能够给你点启发和帮助。
相关文章:
&原文链接:
其他参考文献:
阅读(...) 评论()

我要回帖

更多关于 propertygrid1 的文章

 

随机推荐