Databinding with Nested Objects

V

Victor Jones

Hi

I was wondering if there was any approach to bind an object that contains a
nested object within. Suppose class A is defined as

class A
{
 
T

Thomas Tomiczek \(MVP\)

You really love asking the same question separatly in two newsgroups

Bear with me when I say I dont like ansering them in two newsgroups. I
answered to your post in the databinding newsgroup.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/:NET)
 
V

Victor Jones

Thanks for the suggestion. Doesn't hurt to get different opinions on
different newsgroups ;)


Thomas Tomiczek (MVP) said:
You really love asking the same question separatly in two newsgroups

Bear with me when I say I dont like ansering them in two newsgroups. I
answered to your post in the databinding newsgroup.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/:NET)

Victor Jones said:
Hi

I was wondering if there was any approach to bind an object that
contains
a
nested object within. Suppose class A is defined as

class A
{
.
.
.
//Property that exposes nested object
public B prop
{
get{return new B();}
}
}

and class B is defined as

class B
{
//Properties corresponding to B
public int x
{
get{return 1;}
}
.
.
.

}

If I were to bind a strongly typed collection of object A and try to bind
the collection, the property that exposes the nested object, does not show
the values. What would I require to do if I want to bind collection of A
where the properties of B are included as a "joined view" . I tried
implementing TypeConvertor and override GetProperties, but Iam unable to add
to the propertydescriptorcollection since it is not implemented.

I was wondering if there was any other approach to this whole problem. This
seems like a common scenario.

public class NestedPropertyConverter : TypeConverter
{
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context,object value, Attribute[]
filter)
{
//Obtain the default property descriptors, add nested properties and return
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value,
filter);

for(int i=0; i<props.Count; ++i)
{
PropertyDescriptor desc = props;
if(desc.PropertyType == Type.GetType("B"))
{

PropertyDescriptorCollection nestedProps =
TypeDescriptor.GetProperties(desc.PropertyType);

for(int j=0; j<nestedProps.Count; ++j)
{
PropertyDescriptor nestedDesc = nestedProps[j];
//Add to the top most PropertyDescriptor Collection
//CANNOT ADD SINCE NOT IMPLEMENTED
props.Add(nestedDesc);


}

}
return props;
}
}

Thanks in advance
Viktor

 
T

Thomas Tomiczek \(MVP\)

Does not get hurt, no.

But it shows you dont care for the work other do and you are not a friendly
person.

Why did you not CROSSPOST as netiquette asks for?

Frankly, dont you know how to use a newsreader?

You multiposted, and this is frowned upon.

Thomas Tomiczek

Victor Jones said:
Thanks for the suggestion. Doesn't hurt to get different opinions on
different newsgroups ;)


Thomas Tomiczek (MVP) said:
You really love asking the same question separatly in two newsgroups

Bear with me when I say I dont like ansering them in two newsgroups. I
answered to your post in the databinding newsgroup.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/:NET)

Victor Jones said:
Hi

I was wondering if there was any approach to bind an object that
contains
a
nested object within. Suppose class A is defined as

class A
{
.
.
.
//Property that exposes nested object
public B prop
{
get{return new B();}
}
}

and class B is defined as

class B
{
//Properties corresponding to B
public int x
{
get{return 1;}
}
.
.
.

}

If I were to bind a strongly typed collection of object A and try to bind
the collection, the property that exposes the nested object, does not show
the values. What would I require to do if I want to bind collection of A
where the properties of B are included as a "joined view" . I tried
implementing TypeConvertor and override GetProperties, but Iam unable
to
add
to the propertydescriptorcollection since it is not implemented.

I was wondering if there was any other approach to this whole problem. This
seems like a common scenario.

public class NestedPropertyConverter : TypeConverter
{
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context,object value, Attribute[]
filter)
{
//Obtain the default property descriptors, add nested properties and return
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(value,
filter);

for(int i=0; i<props.Count; ++i)
{
PropertyDescriptor desc = props;
if(desc.PropertyType == Type.GetType("B"))
{

PropertyDescriptorCollection nestedProps =
TypeDescriptor.GetProperties(desc.PropertyType);

for(int j=0; j<nestedProps.Count; ++j)
{
PropertyDescriptor nestedDesc = nestedProps[j];
//Add to the top most PropertyDescriptor Collection
//CANNOT ADD SINCE NOT IMPLEMENTED
props.Add(nestedDesc);


}

}
return props;
}
}

Thanks in advance
Viktor


 

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