Propertygrid and objects within objects

  • Thread starter Marek Wasilewski
  • Start date
M

Marek Wasilewski

Hi
I have an object whose properties are exposed in a property grid using the
usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of
the first object (objMain), thereby getting all of the properties for the
objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would
somehow reconcile itself. Is there any way that something like this could
work?

Thanks for any help anyone can provide.

Marek
 
S

Sean Hederman

Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is MainMemberType))
{
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

Just as a note, if you want this to apply to the property of objMain, rather
than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.
 
M

Marek Wasilewski

Hi Sean
First of all that did the trick - thank you.

But, as with most of these things, it has exposed another question.
objMainMember then has another property objIProperty, which is a class that
implements an iterface (created using CreateInstance on the fly). I now
need to expose this object to the property grid. I repeated the procedure
you described on my derived class, but the only property I get is the name
of objIProperty, with an associated dropdown which only contains (none).

The class in question looks something like this:
[TypeConverter(typeof(MyRealObjConverter))]
public class MyRealObj : IBaseInterface

I implement the methods required by the interface and then add some more
properties, which are the ones I really need to expose to the property grid.

Is this something that can be done?

Best regards,

Marek


Sean Hederman said:
Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is MainMemberType))
{
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

Just as a note, if you want this to apply to the property of objMain, rather
than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.

--
Sean Hederman

http://codingsanity.blogspot.com

Marek Wasilewski said:
Hi
I have an object whose properties are exposed in a property grid using the
usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of
the first object (objMain), thereby getting all of the properties for the
objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would
somehow reconcile itself. Is there any way that something like this could
work?

Thanks for any help anyone can provide.

Marek
 
S

Sean Hederman

Hmm,

I'd say that you probably have to apply the TypeConverter to either the
interface or the property. I haven't done this before (TypeConverters on
interfaces), so I might be very wrong here.

Regards

--
Sean Hederman

http://codingsanity.blogspot.com

Marek Wasilewski said:
Hi Sean
First of all that did the trick - thank you.

But, as with most of these things, it has exposed another question.
objMainMember then has another property objIProperty, which is a class
that
implements an iterface (created using CreateInstance on the fly). I now
need to expose this object to the property grid. I repeated the procedure
you described on my derived class, but the only property I get is the name
of objIProperty, with an associated dropdown which only contains (none).

The class in question looks something like this:
[TypeConverter(typeof(MyRealObjConverter))]
public class MyRealObj : IBaseInterface

I implement the methods required by the interface and then add some more
properties, which are the ones I really need to expose to the property
grid.

Is this something that can be done?

Best regards,

Marek


Sean Hederman said:
Marek,

You need to decorate the type of objMainMember with a
TypeConverterAttribute, referencing a TypeConverter type. This type
converter inherits from TypeConverter, with overrides similar to those
below:
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return false;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ((destinationType == typeof(string)) && (value is MainMemberType))
{
// Return String representation of MainMemberType value
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(MainMemberType),
attributes);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

Just as a note, if you want this to apply to the property of objMain, rather
than all objects which have properties of type MainMemberType, then apply
the TypeConverterAttribute to the property of MainType.

--
Sean Hederman

http://codingsanity.blogspot.com

Marek Wasilewski said:
Hi
I have an object whose properties are exposed in a property grid using the
usual method:

_propertyGrid.SelectedObject = objMain;

However, what I want to do is for the property grid to also display the
properties of an object (objMainMember) which is defined as a property of
the first object (objMain), thereby getting all of the properties for the
objMain and objMainMember objects in one SelectedObject assignment.

I was hoping that by exposing the member object that the whole thing would
somehow reconcile itself. Is there any way that something like this could
work?

Thanks for any help anyone can provide.

Marek
 

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