首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertyDescriptor与属性

PropertyDescriptor与属性
EN

Stack Overflow用户
提问于 2013-07-29 09:02:50
回答 1查看 5.2K关注 0票数 1

我继承了PropertyDescriptor类以提供某种“动态”属性。我正在向PropertyDescriptor添加一些属性。这工作得很好。

PropertyGrid中显示对象时,ReadOnlyAttribute可以工作,但是EditorAttribute不能工作!

代码语言:javascript
复制
internal class ParameterDescriptor: PropertyDescriptor {
    //...
    public ParameterDescriptor(/* ... */) {
        List<Attribute> a = new List<Attribute>();
        string editor = "System.ComponentModel.Design.MultilineStringEditor,System.Design";
        //...
        a.Add(new ReadOnlyAttribute(true));                         // works
        a.Add(new DescriptionAttribute("text"));                    // works
        a.Add(new EditorAttribute(editor, typeof(UITypeEditor)));   // doesn't work!
        //...    
        this.AttributeArray = a.ToArray();
    }
}

显示的对象使用继承的TypeConverter

代码语言:javascript
复制
public class ParameterBoxTypeConverter: TypeConverter {
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
        return true;
    }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
        List<PropertyDescriptor> desc = new List<PropertyDescriptor>();
        //...
        ParameterDescriptor d = new ParameterDescriptor(/* ... */);
        desc.Add(d);
        //....
        return new PropertyDescriptorCollection(desc.ToArray());
    }

我被困住了,因为PropertyGrid根本没有显示任何东西(我希望有一个“.”按属性值计算)。而且似乎没有办法进行调试!

那我怎么才能找到这里出了什么问题?

有办法调试到PropertyGrid等吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-29 09:21:55

从几个快速测试中,这个名称需要被定义为:

代码语言:javascript
复制
const string name = "System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
attribs.Add(new EditorAttribute(name, typeof(UITypeEditor)));

在内部,它使用Type.GetType,并且:

代码语言:javascript
复制
var type1 = Type.GetType("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
// ^^^ not null
var type2 = Type.GetType("System.ComponentModel.Design.MultilineStringEditor, System.Design");
// ^^^ null

当然,你可以用:

代码语言:javascript
复制
attribs.Add(new EditorAttribute(typeof(MultilineStringEditor), typeof(UITypeEditor)));

或者,您可以override GetEditor并做您想做的任何事情。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17919881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档