我制作了以下定制的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中的所有新属性都是ReadOnly!!我不明白为什么!?我尝试了所有的方法,我也在CustomProperty的IsReadOnly属性中放了一个return false;,但是没有办法。属性在PropertGrid中始终显示为ReadOnly。
有谁有主意吗?
发布于 2013-03-15 04:41:56
您的PropertyType和ComponentType实现失败了。它们应该返回内部属性的PropertyType / ComponentType。通过返回GetType,您返回的是ReflectionPropertyDescriptor之类的内容,它既不能编辑,也不能转换。
public override Type PropertyType
{
get { return _innerPropertyDescriptor.PropertyType; }
}https://stackoverflow.com/questions/15419387
复制相似问题