This project has moved and is read-only. For the latest updates, please go here.

How do I embed a PropertyGrid in a user control and still set custom editors

May 16, 2012 at 9:16 PM


We would like to embed the PropertyGrid in a specialized UserControl for use in multiple projects. The problem is getting to the customized editors in that scenario. Here is an example of working Xaml without using it in a user control:

    <xctk:PropertyGrid DockPanel.Dock="Top" Name="propertyGrid" HorizontalAlignment="Stretch" Margin="0,6,6,6"
                    <xctk:PropertyDefinition Name="Rows" />
                    <xctk:PropertyDefinition Name="Columns" />
                        <xctk:IntegerUpDown Minimum="1" Maximum="50" DefaultValue="1" Text="{Binding Value}" />
I tried to set up dependency properties in the new user control for the EditorDefinitionCollection
and EditorDefintion but could not get them to work properly. So I tried setting them in code just to
try and get them to work using something similar to the following, but I do not know how to set up a
DataTemplate in code. But the factory.SetValue methods below don't change anything.
this.propertyGridUserControl1.EditorDefinitions = new EditorDefinitionCollection();

EditorDefinition editorDefinition = new EditorDefinition();
editorDefinition.PropertiesDefinitions.Add(new PropertyDefinition() { Name = "Rows" });
editorDefinition.PropertiesDefinitions.Add(new PropertyDefinition() { Name = "Columns" });

DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(IntegerUpDown));
factory.Name = "integerUpDownFactory";
factory.SetValue(IntegerUpDown.MinimumProperty, 1);
factory.SetValue(IntegerUpDown.MaximumProperty, 50);
factory.SetValue(IntegerUpDown.DefaultValueProperty, 1);

Binding binding = new Binding();
binding.ElementName = "integerUpDownFactory";
binding.Path = new PropertyPath(IntegerUpDown.ValueProperty);

factory.SetBinding(IntegerUpDown.TextProperty, binding);

dataTemplate.VisualTree = factory;

editorDefinition.EditorTemplate = dataTemplate;