如何动态或在运行时设置PropertyGrid的DefaultValueAttribute? [英] How to set DefaultValueAttribute of the PropertyGrid dynamically or at runtime?

查看:44
本文介绍了如何动态或在运行时设置PropertyGrid的DefaultValueAttribute?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义要与 PropertyGrid 一起使用的自定义类.控制.假设属性之一定义如下:

I am defining a custom class to be used with the PropertyGrid control. Say, one of the properties is defined as such:

[CategoryAttribute("Section Name"),
DefaultValueAttribute("Default value"),
DescriptionAttribute("My property description")]
public string MyPropertyName
{
    get { return _MyPropertyName; }
    set { _MyPropertyName = value; }
}

private string _MyPropertyName;

如您所见, DefaultValueAttribute 定义了属性的默认值.这种默认值在两种情况下使用:

As you see DefaultValueAttribute defines a default value for the property. Such default value is used in two cases:

  1. 如果此属性值从默认值更改,则 PropertyGrid 控件将其以粗体显示,并且

  1. If this property value is changed from the default one the PropertyGrid control will display it in bold, and

如果我调用 PropertyGrid ResetSelectedProperty 方法,它将默认值应用于选定的单元格.

If I call ResetSelectedProperty method of the PropertyGrid, it will apply that default value to a selected cell.

除了 DefaultValueAttribute 的一个限制外,此概念很好用.它仅接受一个恒定值.所以我很好奇,我可以动态地(例如,从构造函数中或在代码的后面)设置它吗?

This concept works fine, except one limitation of the DefaultValueAttribute. It accepts only a constant value. So I'm curious, can I set it dynamically, say, from a constructor or later in the code?

编辑:我能够找到

I was able to find this code that lets me read the DefaultValueAttribute:

AttributeCollection attributes = TypeDescriptor.GetProperties(this)["MyPropertyName"].Attributes;
DefaultValueAttribute myAttribute = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
string strDefaultValue = (string)myAttribute.Value;

问题是,如何设置?

推荐答案

最后,我得到了答案!我已经进入了很多网站,展示了如何实现 ICustomTypeDescriptor PropertyDescriptor (

Finally, I got the answer! I've been running into a bunch of sites showing how to implement ICustomTypeDescriptor and PropertyDescriptor (here's one), which is fine if you want to add literally two pages of code to your 10-line class.

这是一种更快的方法.我发现了一个提示

Here's a much faster way. I found a hint here. Bless those who actually post constructive ideas!

所以答案是在您的类中提供两个方法.一个是 private bool ShouldSerializePPP(),另一个是 private void ResetPPP(),其中 PPP 是您的属性名称. PropertyGrid 将调用前一种方法,以确定属性值是否从默认值更改,而每当 PropertyGrid 项目重置为默认值时,都会调用后一种方法默认值.

So the answer is to provide two methods in your class. One is private bool ShouldSerializePPP() and another one is private void ResetPPP() where PPP is your property name. The former method will be called by the PropertyGrid to determined if the property value was changed from a default one, and the latter method will be called whenever the PropertyGrid item is reset to a default value.

这是我的类在添加这些内容后的外观,这将允许在运行时为属性设置默认值:

Here's how my class should look with these additions, that will allow to set a default value for a property at a run-time:

[CategoryAttribute("Section Name"),
DescriptionAttribute("My property description")]
public string MyPropertyName
{
    get { return _MyPropertyName; }
    set { _MyPropertyName = value; }
}
private bool ShouldSerializeMyPropertyName()
{
    //RETURN:
    //      = true if the property value should be displayed in bold, or "treated as different from a default one"
    return !(_MyPropertyName == "Default value");
}
private void ResetMyPropertyName()
{
    //This method is called after a call to 'PropertyGrid.ResetSelectedProperty()' method on this property
   _MyPropertyName = "Default value";
}

private string _MyPropertyName;

这篇关于如何动态或在运行时设置PropertyGrid的DefaultValueAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆