Populating a PropertyGrid using reflected types. 
Author Message
 Populating a PropertyGrid using reflected types.

We want to populate a PropertyGrid dynamically at run time.

The property grid will not have a handle to a concrete object. The
properties of the classes in question will have the correct attributes on
them, but will need to be discovered by reflecting on the classes at run
time.

We will end up with an array of PropertyInfo objects that need to fill up
the PropertyGrid. Can we do this? Or does the PropertyGrid require something
more concrete to work with? If it can be done, are there any code examples
out there?

Cheers,

4Space



Sat, 25 Jun 2005 16:18:25 GMT  
 Populating a PropertyGrid using reflected types.
4Space,

    The property grid uses reflection to determine what to show in the grid,
so you shouldn't have a problem just setting it to your object.  What do you
mean thought by not having a handle to a concrete object?

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> We want to populate a PropertyGrid dynamically at run time.

> The property grid will not have a handle to a concrete object. The
> properties of the classes in question will have the correct attributes on
> them, but will need to be discovered by reflecting on the classes at run
> time.

> We will end up with an array of PropertyInfo objects that need to fill up
> the PropertyGrid. Can we do this? Or does the PropertyGrid require
something
> more concrete to work with? If it can be done, are there any code examples
> out there?

> Cheers,

> 4Space



Sat, 25 Jun 2005 22:15:38 GMT  
 Populating a PropertyGrid using reflected types.

Quote:
>     The property grid uses reflection to determine what to show in the
grid,
> so you shouldn't have a problem just setting it to your object.  What do
you
> mean thought by not having a handle to a concrete object?

>                - Nicholas Paldino [.NET/C# MVP]

Hi Nicholas.

At runtime, the PropertyGrid is going to be pointed at a collection of
'System.Object's. Each object is of a completely different type, and with a
completely different interface. That's all it knows. It has to reflect on
each object, and get a list of properties for it. Any properties that have
the appropriate attribute on them should be displayed on the PropertyGrid.

Can it be done?

Cheers,

4Space



Sat, 25 Jun 2005 22:55:00 GMT  
 Populating a PropertyGrid using reflected types.

4Space,

    Since the property grid takes either an object, or an array of objects
to display the property for, it has to use some kind of mechanism to
determine the properties.  This mechanism is reflection.  As for displaying
attributes, the property grid will not display them.  It will honor some
attributes when it comes to leaving out certain properties and the like.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> >     The property grid uses reflection to determine what to show in the
> grid,
> > so you shouldn't have a problem just setting it to your object.  What do
> you
> > mean thought by not having a handle to a concrete object?

> >                - Nicholas Paldino [.NET/C# MVP]

> Hi Nicholas.

> At runtime, the PropertyGrid is going to be pointed at a collection of
> 'System.Object's. Each object is of a completely different type, and with
a
> completely different interface. That's all it knows. It has to reflect on
> each object, and get a list of properties for it. Any properties that have
> the appropriate attribute on them should be displayed on the PropertyGrid.

> Can it be done?

> Cheers,

> 4Space



Sat, 25 Jun 2005 23:06:31 GMT  
 Populating a PropertyGrid using reflected types.
4Space,

I don't know if it can be done for a collection of objects but for one
object it can be done in the following way:

using System;
using System.ComponentModel;
using System.Windows.Forms;
// ...

// the PropertyGrid:
PropertyGrid pg = new PropertyGrid();
// some more initialization probably here....

// the object:
YourObject o1 = new YourObject();
// ...

pg.SelectedObject = o1;

// show only the DesignTime attributes
// (by default all the properties are DesignTime(false),
// so only the explicitly marked ones will be shown):
foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(o1))
{

if(((DesignOnlyAttribute)pd.Attributes[typeof(DesignOnlyAttribute)]).IsDesig
nOnly)
 {
  pg.BrowsableAttributes = new AttributeCollection(new Attribute[]{
         pd.Attributes[typeof(DesignOnlyAttribute)]});
  break;
 }

Quote:
}

If you change the SelectedObject of the PropertyGrid, for the newly selected
object again only the DesignTime properties will be shown, i.e.:

SomeObject o2 = new SomeObject();
pg.SelectedObject = o2;
// only the DesignTime(true) properties will be shown

This may lead to the effect that this code could do what you need for the
collection of objects you need to be displayed, but I don't have time now to
test it, either to find it documented.

Also, the Browsable attribute is not taken in consideration, i.e. a
DesignOnly(true) and Browsable(false) property will be shown in the grid.

Hope this helps
Marty

Quote:
> >     The property grid uses reflection to determine what to show in the
> grid,
> > so you shouldn't have a problem just setting it to your object.  What do
> you
> > mean thought by not having a handle to a concrete object?

> >                - Nicholas Paldino [.NET/C# MVP]

> Hi Nicholas.

> At runtime, the PropertyGrid is going to be pointed at a collection of
> 'System.Object's. Each object is of a completely different type, and with
a
> completely different interface. That's all it knows. It has to reflect on
> each object, and get a list of properties for it. Any properties that have
> the appropriate attribute on them should be displayed on the PropertyGrid.

> Can it be done?

> Cheers,

> 4Space



Sun, 26 Jun 2005 00:29:21 GMT  
 Populating a PropertyGrid using reflected types.
Hello,

You need an object, but it can be an empty one, and its TypeConverter (set
with attr) determines what goes in the grid. So there does not have to be
any correspondance between the real props and the ones in the grid. Check
out the samples here:

http://msdn.microsoft.com/library/en-us/dndotnet/html/vsnetpropbrow.asp

Regards,
Frank Hileman
Prodige Software Corporation


Quote:
> We want to populate a PropertyGrid dynamically at run time.

> The property grid will not have a handle to a concrete object. The
> properties of the classes in question will have the correct attributes on
> them, but will need to be discovered by reflecting on the classes at run
> time.

> We will end up with an array of PropertyInfo objects that need to fill up
> the PropertyGrid. Can we do this? Or does the PropertyGrid require
something
> more concrete to work with? If it can be done, are there any code examples
> out there?



Sun, 26 Jun 2005 07:18:46 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Using types in a different assembly given that the type may be used or not used

2. Bug? ExecutionEngineException reflecting boxed type in MC++

3. PropertyGrid and properties with object types

4. Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton

5. Using C# PropertyGrids

6. dynamically populate listbox using CDHtmlDialog

7. Populating a treeview from a database using Webforms

8. Populating XML into Treeview using WTL 7.0

9. Can't populate Access Memo field using Win32 api

10. using incomplete structure types as opaque types

11. Transaction property changes not getting reflected in Component Services

12. browser address doesn't reflect current web form

 

 
Powered by phpBB® Forum Software