P
Polo
Hi,
I have a Custom ExpandableTypeConverter (that show the property of a object
readonly)
Some properties of this sub object have the Browsable(false) attribute but
in the PropertyGrid these properties are showed.
public class Test
{
//....
[TypeConverter(typeof(ReadOnlyExpandableObjectConverter ))]
public SubTest Sub
{
// ....
}
}
public class SubTest
{
int _V = 0;
//....
[Browsable(false)]
int V
{
get { return _V;}
set {_V = value; }
}
}
internal class ReadOnlyPropertyDescriptor : PropertyDescriptor
{
PropertyDescriptor _PropertyDescriptor;
public ReadOnlyPropertyDescriptor(PropertyDescriptor propertyDescriptor) :
base(propertyDescriptor)
{
_PropertyDescriptor = propertyDescriptor;
}
public override bool IsReadOnly
{
get { return true; }
}
public override bool IsBrowsable
{
get {return this._PropertyDescriptor.IsBrowsable; }
}
public override object GetValue(object component)
{
return this._PropertyDescriptor.GetValue(component);
}
public override string Name
{
get { return this._PropertyDescriptor.Name; }
}
public override Type PropertyType
{
get { return this._PropertyDescriptor.PropertyType; }
}
public override void ResetValue(object component)
{
this._PropertyDescriptor.ResetValue(component);
}
public override bool ShouldSerializeValue(object component)
{
return this._PropertyDescriptor.ShouldSerializeValue(component);
}
public override void SetValue(object component, object value)
{
this._PropertyDescriptor.SetValue(component, value);
}
public override bool CanResetValue(object component)
{
return this._PropertyDescriptor.CanResetValue(component);
}
public override Type ComponentType
{
get { return this._PropertyDescriptor.ComponentType; }
}
public override AttributeCollection Attributes
{
get { return this._PropertyDescriptor.Attributes; }
}
}
internal class ReadOnlyExpandableObjectConverter :
ExpandableObjectConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext
context)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value,
true);
PropertyDescriptorCollection newProps = new
PropertyDescriptorCollection(null);
foreach(PropertyDescriptor p in props)
{
newProps.Add(new ReadOnlyPropertyDescriptor(p));
}
return newProps;
}
// .....
}
I have a Custom ExpandableTypeConverter (that show the property of a object
readonly)
Some properties of this sub object have the Browsable(false) attribute but
in the PropertyGrid these properties are showed.
public class Test
{
//....
[TypeConverter(typeof(ReadOnlyExpandableObjectConverter ))]
public SubTest Sub
{
// ....
}
}
public class SubTest
{
int _V = 0;
//....
[Browsable(false)]
int V
{
get { return _V;}
set {_V = value; }
}
}
internal class ReadOnlyPropertyDescriptor : PropertyDescriptor
{
PropertyDescriptor _PropertyDescriptor;
public ReadOnlyPropertyDescriptor(PropertyDescriptor propertyDescriptor) :
base(propertyDescriptor)
{
_PropertyDescriptor = propertyDescriptor;
}
public override bool IsReadOnly
{
get { return true; }
}
public override bool IsBrowsable
{
get {return this._PropertyDescriptor.IsBrowsable; }
}
public override object GetValue(object component)
{
return this._PropertyDescriptor.GetValue(component);
}
public override string Name
{
get { return this._PropertyDescriptor.Name; }
}
public override Type PropertyType
{
get { return this._PropertyDescriptor.PropertyType; }
}
public override void ResetValue(object component)
{
this._PropertyDescriptor.ResetValue(component);
}
public override bool ShouldSerializeValue(object component)
{
return this._PropertyDescriptor.ShouldSerializeValue(component);
}
public override void SetValue(object component, object value)
{
this._PropertyDescriptor.SetValue(component, value);
}
public override bool CanResetValue(object component)
{
return this._PropertyDescriptor.CanResetValue(component);
}
public override Type ComponentType
{
get { return this._PropertyDescriptor.ComponentType; }
}
public override AttributeCollection Attributes
{
get { return this._PropertyDescriptor.Attributes; }
}
}
internal class ReadOnlyExpandableObjectConverter :
ExpandableObjectConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext
context)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value,
true);
PropertyDescriptorCollection newProps = new
PropertyDescriptorCollection(null);
foreach(PropertyDescriptor p in props)
{
newProps.Add(new ReadOnlyPropertyDescriptor(p));
}
return newProps;
}
// .....
}