如何让PropertyGrid显示控件的Name属性
2个回答
展开全部
这是核心代码:
class ControlDescriptionProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider =
TypeDescriptor.GetProvider(typeof(Control));
public ControlDescriptionProvider() : base(defaultTypeProvider) { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
return new TitleCustomTypeDescriptor(defaultDescriptor);
}
}
class TitleCustomTypeDescriptor : CustomTypeDescriptor
{
public TitleCustomTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{
customFields.Add(new NamePropertyDescriptor());
}
private List<PropertyDescriptor> customFields = new List<PropertyDescriptor>();
public override PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(base.GetProperties()
.Cast<PropertyDescriptor>().Union(customFields).ToArray());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return new PropertyDescriptorCollection(base.GetProperties(attributes)
.Cast<PropertyDescriptor>().Union(customFields).ToArray());
}
}
class NamePropertyDescriptor : PropertyDescriptor
{
public NamePropertyDescriptor() : base("(Name)", null) { }
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get
{
return typeof(Control);
}
}
public override object GetValue(object component)
{
Control control = (Control)component;
return control.Name;
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override Type PropertyType
{
get
{
return typeof(string);
}
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
Control control = (Control)component;
control.Name = value.ToString();
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
下面是测试代码:
ControlDescriptionProvider provider = new ControlDescriptionProvider();
TypeDescriptor.AddProvider(provider, typeof(Control));
//下面测试下Label,任何控件都可以。
Label label = new Label();
label.Name = "123";
propertyGrid1.SelectedObject = label;
class ControlDescriptionProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider =
TypeDescriptor.GetProvider(typeof(Control));
public ControlDescriptionProvider() : base(defaultTypeProvider) { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
return new TitleCustomTypeDescriptor(defaultDescriptor);
}
}
class TitleCustomTypeDescriptor : CustomTypeDescriptor
{
public TitleCustomTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{
customFields.Add(new NamePropertyDescriptor());
}
private List<PropertyDescriptor> customFields = new List<PropertyDescriptor>();
public override PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(base.GetProperties()
.Cast<PropertyDescriptor>().Union(customFields).ToArray());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return new PropertyDescriptorCollection(base.GetProperties(attributes)
.Cast<PropertyDescriptor>().Union(customFields).ToArray());
}
}
class NamePropertyDescriptor : PropertyDescriptor
{
public NamePropertyDescriptor() : base("(Name)", null) { }
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get
{
return typeof(Control);
}
}
public override object GetValue(object component)
{
Control control = (Control)component;
return control.Name;
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override Type PropertyType
{
get
{
return typeof(string);
}
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
Control control = (Control)component;
control.Name = value.ToString();
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
下面是测试代码:
ControlDescriptionProvider provider = new ControlDescriptionProvider();
TypeDescriptor.AddProvider(provider, typeof(Control));
//下面测试下Label,任何控件都可以。
Label label = new Label();
label.Name = "123";
propertyGrid1.SelectedObject = label;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-01
展开全部
Button btn=new Button();
btn.Name="MyName";
btn.Text="OK";
propertyGrid1.SelectedObject=btn;
在propertyGrid1控件上,会显示btn的Text属性会OK,但是不会显示Name属性,我希望把Name属性也要显示出来
因为我的SelectedObject在运行时可能是不同的控件,所以不能通过从Button派生出自己的自定义Button来实现
btn.Name="MyName";
btn.Text="OK";
propertyGrid1.SelectedObject=btn;
在propertyGrid1控件上,会显示btn的Text属性会OK,但是不会显示Name属性,我希望把Name属性也要显示出来
因为我的SelectedObject在运行时可能是不同的控件,所以不能通过从Button派生出自己的自定义Button来实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询