CustomPropertyDescriptor AND BrowsableAttribute problem

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;
}
// .....
}
 
G

Guest

Hi Polo!
I have quite the same problem. I did a few tests to find out what the
problem is.
I think it is a problem of the propertygrid. It doesn't call the IsBrowsable
for the descriptor. For the IsReadOnly it works.
If you place a breakpoint in the IsBrowsable property you see that it is
never called.

maybe you can post this problem in the WinForms group.
hotdi

Polo said:
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;
}
// .....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top