1

我做了以下自定义 PropertyDescriptor

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 将用于以下类

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 中的所有新属性都是只读的!我不明白为什么!?我什么都试过了,我也放了一个return false;属性IsReadOnlyCustomProperty但没办法。属性在 PropertGrid 中始终显示为只读。

有人有想法吗?

4

1 回答 1

1

您的 PropertyType 和 ComponentType 实现已损坏。他们应该返回内部属性的 PropertyType / ComponentType。通过返回 GetType,您将返回类似于 ReflectionPropertyDescriptor 的东西,它既不可编辑也不可转换。

    public override Type PropertyType
    {
        get { return _innerPropertyDescriptor.PropertyType; }
    }
于 2013-03-14T20:41:56.367 回答