首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义PropertyDescriptor始终为只读

自定义PropertyDescriptor始终为只读
EN

Stack Overflow用户
提问于 2013-03-15 04:20:12
回答 1查看 2.1K关注 0票数 1

我制作了以下定制的PropertyDescriptor

代码语言:javascript
复制
public class CustomProperty : PropertyDescriptor
{
    private PropertyDescriptor _innerPropertyDescriptor;
    private bool _ronly;

    public CustomProperty(PropertyDescriptor inner, Attribute[] attrs)
        : base(inner.Name, attrs)
    {
        _innerPropertyDescriptor = inner;
        _ronly = inner.IsReadOnly;           
    }
    public override object GetValue(object component)
    {
        return _innerPropertyDescriptor.GetValue(component);
    }
    public override bool SupportsChangeEvents
    {
        get { return true; }
    }

    public override Type PropertyType
    {
        get { return _innerPropertyDescriptor.GetType(); }
    }

    public override void ResetValue(object component)
    {
        // Not relevant.
    }

    public override void SetValue(object component, object value)
    {
        _innerPropertyDescriptor = (CustomProperty)value;
    }

    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }

    public override bool CanResetValue(object component)
    {
        return true;
    }

    public override Type ComponentType
    {
        get { return _innerPropertyDescriptor.GetType(); }
    }
    public override bool IsReadOnly
    {
        get
        {
            return false;
        }
    }
}

此PropertyDescriptor将用于以下类

代码语言:javascript
复制
public class MyClass : ICustomTypeDescriptor
{
    #region MyClass Properties
    ......
    #endregion
    #region ICustomTypeDescriptor Implementation
    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this,true);
    }
    public string GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }
    public string GetComponentName()
    {
        return TypeDescriptor.GetComponentName(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(System.Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this,editorBaseType, true);
    }
    public EventDescriptorCollection GetEvents(System.Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this,attributes, true);
    }
    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }
    public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)
    {
        PropertyDescriptorCollection originalCollection = TypeDescriptor.GetProperties(this,attributes,true);
        PropertyDescriptor[] pds = new PropertyDescriptor[originalCollection.Count];
        originalCollection.CopyTo(pds,0);
        PropertyDescriptorCollection newCollection = new PropertyDescriptorCollection(pds);
        for (int i = 0; i < originalCollection.Count; i++)
        {
            PropertyDescriptor pd = originalCollection[i];
            List<Attribute> la = new List<Attribute>();
            foreach (Attribute attribute in pd.Attributes)
                la.Add(attribute);
            CustomProperty cp = new CustomProperty(pd, la.ToArray());                
            newCollection.RemoveAt(i);
            newCollection.Insert(i, cp);
        }         
        return newCollection;
    }
    public PropertyDescriptorCollection GetProperties()
    {
        return TypeDescriptor.GetProperties(this, true);
    }
    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }
    #endregion
}

我在此实现中所做的是重写MyClass属性,以便能够从Visual Studio PropertyGrid管理重置功能。看起来一切正常,但是这个实现产生了错误的效果:我存储在PropertyDescriptorCollection中的所有新属性都是ReadOnly!!我不明白为什么!?我尝试了所有的方法,我也在CustomPropertyIsReadOnly属性中放了一个return false;,但是没有办法。属性在PropertGrid中始终显示为ReadOnly。

有谁有主意吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-15 04:41:56

您的PropertyType和ComponentType实现失败了。它们应该返回内部属性的PropertyType / ComponentType。通过返回GetType,您返回的是ReflectionPropertyDescriptor之类的内容,它既不能编辑,也不能转换。

代码语言:javascript
复制
    public override Type PropertyType
    {
        get { return _innerPropertyDescriptor.PropertyType; }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15419387

复制
相关文章

相似问题

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