我制作了以下自定义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管理Reset功能。
一切似乎都运行良好,但这种实现会导致错误的效果:存储在PropertyDescriptorCollection中的所有新属性都是ReadOnly !!我无法理解为什么!?我尝试了所有内容,我在return false;
的{{1}}属性中添加了IsReadOnly
,但没办法。属性在PropertGrid中始终显示为ReadOnly。
有人有想法吗?
答案 0 :(得分:1)
您的PropertyType和ComponentType实现已损坏。他们应该返回内部属性的PropertyType / ComponentType。通过返回GetType,您将返回类似ReflectionPropertyDescriptor的内容,该内容既不可编辑也不可转换。
public override Type PropertyType
{
get { return _innerPropertyDescriptor.PropertyType; }
}