This project has moved. 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 8:16 PM

Hello,

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"
                               PropertyValueChanged="propertyGrid_PropertyValueChanged">
        <xctk:PropertyGrid.EditorDefinitions>
            <xctk:EditorDefinition>
                <xctk:EditorDefinition.PropertiesDefinitions>
                    <xctk:PropertyDefinition Name="Rows" />
                    <xctk:PropertyDefinition Name="Columns" />
                </xctk:EditorDefinition.PropertiesDefinitions>
                <xctk:EditorDefinition.EditorTemplate>
                    <DataTemplate>
                        <xctk:IntegerUpDown Minimum="1" Maximum="50" DefaultValue="1" Text="{Binding Value}" />
                    </DataTemplate>
                </xctk:EditorDefinition.EditorTemplate>
            </xctk:EditorDefinition>
        </xctk:PropertyGrid.EditorDefinitions>
    </xctk:PropertyGrid>
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;
this.propertyGridUserControl1.EditorDefinitions.Add(editorDefinition);