By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

Propertygrid and objects within objects

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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" <us*******@doesntexist.com> wrote in message
news:cr**********@ctb-nnrp2.saix.net...
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" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #3

P: n/a
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" <ma**************@dnv.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
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" <us*******@doesntexist.com> wrote in message
news:cr**********@ctb-nnrp2.saix.net...
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" <ma**************@dnv.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
> 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
>
>



Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.