Custom property editors in the WPF Property Grid
I wrote previously about editing custom types in the WPF Property Grid — how to extend the grid to provide a good editing experience for, say, PhoneNumber objects. But what if you have a particular property which is of a “known” type, but the normal editing experience isn’t appropriate?
Here’s an example. Suppose you have a PrintSettings class with an Orientation property of boolean type. That’s an awful API for you to have to deal with, and even worse for your users:
Quick, does an Orientation of true mean portrait or landscape? Who knows? Even if the designer of the PrintSettings class had used an enum instead of a boolean, you might still want to improve the editing experience by providing pictures as well as the words Portrait and Landscape, so that less print-savvy users would know what the terms meant.
Unfortunately, the type editor extensibility mechanism doesn’t help us in this case. We don’t want to change the editor used for booleans in general, because that would affect the Duplex editor as well: we just want to change it for this specific property.
In the WPF Property Grid, we can do this using property editors. You create property editors in almost exactly the same way as type editors, but they have to be wired up a little differently.
To create our custom property editor, just as with a type editor, all we have to do is create a DataTemplate which can bind to a boolean value:
<DataTemplate x:Key="OrientationEditor"> <ComboBox SelectedValue="{Binding Value}" SelectedValuePath="Tag" BorderThickness="0"> <ComboBoxItem Tag="False"> <StackPanel Orientation="Horizontal"> <Border Width="30" Height="30"> <Rectangle Width="17" Height="22" Stroke="Black" StrokeThickness="1" Fill="LightGray" /> </Border> <TextBlock Text="Portrait" VerticalAlignment="Center" /> </StackPanel> </ComboBoxItem> <ComboBoxItem Tag="True"> <StackPanel Orientation="Horizontal"> <Border Width="30" Height="30"> <Rectangle Width="22" Height="17" Stroke="Black" StrokeThickness="1" Fill="LightGray" /> </Border> <TextBlock Text="Landscape" VerticalAlignment="Center" /> </StackPanel> </ComboBoxItem> </ComboBox> </DataTemplate>
Here’s the first difference from the type editor. In the type editor template, we bound our databound elements to properties of the edited type. (In the PhoneNumber example, we had a text box bound to the CountryCode property.) But a boolean doesn’t have properties. How, then, can we reference it in our DataTemplate? The WPF Property Grid solves this by requiring property editors operating on base types to instead bind to a special pseudo-property called Value. The property grid wires things up so that the Value pseudo-property actually maps to the real property, in this case Orientation.
Now, as we did with our type editor, we have to tell the WPF Property Grid to use this template for editing the PrintSettings.Orientation property. We do this by adding a PropertyEditor declaration to the grid’s Editors property:
<ms:PropertyGrid SelectedObject="{Binding}"> <ms:PropertyGrid.Editors> <ms:PropertyEditor DeclaringType="local:PrintSettings" PropertyName="Orientation" EditorTemplate="{StaticResource OrientationEditor}" /> </ms:PropertyGrid.Editors> </ms:PropertyGrid>
Note the DeclaringType and PropertyName declarations to tell the WPF Property Grid where to use the new editor.
After adding the new editor, the grid looks like this:
Now that’s a lot more meaningful!
Leave a Reply
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (31)
Lab Samples (2)
LightSpeed (132)
MegaPack (3)
News (48)
Products (64)
Projects (4)
Screencast (6)
SharePoint (1)
Silverlight (5)
Silverlight Elements (12)
SimpleDB Management Tools (11)
Visual Studio (4)
VS File Explorer (5)
WPF (31)
WPF Diagramming (14)
WPF Elements (22)
WPF Property Grid (24)
![]()
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007



Tagged as 

Posted by Ivan Towlson on 24 January 2008


