views:

620

answers:

2

I started a hobby project to learn about WPF and in my reasearch i stumpled upon this WPF PropertyGrid http://www.codeplex.com/wpg

I downloaded the source from http://wpg.codeplex.com/SourceControl/ListDownloadableCommits.aspx , And started browsing through it, looks cool, and works. But for the life of me, I can't figure out HOW it works. The following questions is what I'm left with at first glance.

  • How does it know what properties an object have?
  • How does it render the control to edit a Value?
  • How does it decide what control to render? (i'm guessing meta data descriptions on the properties)

I understand the DependancyProperties handles the dataexchange between new values.

  • Where is the XAML or CodeBehind code that does all the magic?

There is lots of XAML code in the Default.xaml file, but as far as I can tell it is only styles and the looks that are defined there.

+3  A: 
  • How does it know what properties an object have?

If it works the same way as the Windows Forms property grid, it does the following :

  • if the object implements ICustomTypeDescriptor, obtain the properties from this interface (with the GetProperties method)
  • otherwise, use reflection on the object's type (obj.GetType().GetProperties()

For the other questions... well, I haven't looked at the code, so I can't answer ;)

Thomas Levesque
+5  A: 
  • How does it know what properties an object have?

It uses System.ComponentModel.TypeDescriptor to determine the properties that a type has via TypeDescriptor.GetProperties. A PropertyDescriptor provides a wealth of information about each property (e.g., PropertyDescriptor.IsReadOnly will tell you that a property is readonly). Further, using PropertyDescriptor.GetValue and PropertyDescriptor.SetValue, the values of properties can be obtained and written to.

  • How does it render the control to edit a Value?
  • How does it decide what control to render? (i'm guessing meta data descriptions on the properties)

It uses a custom control called a PropertyGrid which exposes an ObservableCollection of Items. Item is merely a base class for the core model object which is Property. Property exposes the underlying type (PropertyDescritor.PropertyType) of the property and exposes the PropertyDescriptor.GetValue and PropertyDescriptor.SetValue methods via Property.Value. This, in turn, is what is bound to to enable the editing. DataTemplates are the key here. A few CLR types have custom DataTemplates that render the controls you see. For example, the Boolean type is rendered as a CheckBox while an Enum is rendered as a ComboBox. The default DataTemplate is a TextBox.

  • Where is the XAML or CodeBehind code that does all the magic?

The propensity of it is in Themes\Default.xaml.

The code can be summarized as thus. There is data (Data\Property) that encapsulates the information about properties of an object and provides a property (Property.Value) to read and write the value of a property. This data is exposed as an ObservableCollection in a custom control (PropertyGrid) which is rendered using DataTemplates in Themes\Default.xaml.

There is lots of XAML code in the Default.xaml file, but as far as I can tell it is only styles and the looks that are defined there.

Don't overlook the DataTemplates.

Jason