This project has moved. For the latest updates, please go here.

Property Grid: Displaying enums in combobox

Apr 28, 2014 at 1:43 PM
Hi!

In our propertygrid, I'm currently utilizing the default behavior of displaying an enumeration as a combobox in our default style. But, when I add an editortemplatedefinition, such as this:

<xctk:EditorTemplateDefinition >
            <xctk:EditorTemplateDefinition.TargetProperties>
              <xctk:TargetPropertyType Type="{x:Type sys:Enum}" />
            </xctk:EditorTemplateDefinition.TargetProperties>
            <xctk:EditorTemplateDefinition.EditingTemplate>
              <DataTemplate>
                <ComboBox>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Value}" IsEditable="True"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
                </ComboBox>
              </DataTemplate>
            </xctk:EditorTemplateDefinition.EditingTemplate>
          </xctk:EditorTemplateDefinition>
A lot of things don't seem to work. Can you provide the default template you are using (for an enumeration) if a custom editor template definition isn't supplied?

Or, perhaps, you can answer my real question: I would like to have our combobox display enum strings that come from our resource file so that they can be translatable. I was looking to use a typeconverter, like this: http://stackoverflow.com/questions/796607/how-do-i-have-an-enum-bound-combobox-with-custom-string-formatting-for-enum-valu . Do you have any examples of the best way to do this?

Thanks!
   -Glenn  
Developer
Apr 30, 2014 at 2:19 PM
Hi,

You can find the Style for the default editor of a type Enum in
-Xceed.Wpf.Toolkit/PropertyGrid/Themes/Aero2.NormalColor.xaml (for Windows 8)
-Xceed.Wpf.Toolkit/PropertyGrid/Themes/Generic.xaml ( for other Windows )
It is the style targeting the type "PropertyGridEditorComboBox", which is based on the default ComboBox. So nothing special about it, except using a transparent background/BorderBrush when not focused.

Also, the "Plus" edition of the toolkit supports Localization with Localization attributes. You can have a look at the sample "Data/PropertyGrid/Using SelectedObjects/Localization" available in the "LiveExplorer App" that you can find on this page : https://wpftoolkit.codeplex.com/. Have a look and see if it fills your needs.

In the meantime, here's an idea based on your code snippet:
<Window.Resources>
      <x:Array x:Key="comboSource1"
               Type="sys:String">
         <sys:String>First</sys:String>
         <sys:String>Second</sys:String>
         <sys:String>Third</sys:String>
         <sys:String>Fourth</sys:String>
      </x:Array>
   </Window.Resources>
    
<Grid>
      <xctk:PropertyGrid x:Name="_propertyGrid"
                         SelectedObject="{Binding}">
         <xctk:PropertyGrid.EditorDefinitions>
            <xctk:EditorTemplateDefinition TargetProperties="{x:Type sys:Enum}">
               <xctk:EditorTemplateDefinition.EditingTemplate>
                  <DataTemplate>
                     <ComboBox ItemsSource="{StaticResource comboSource1}"
                               IsEditable="True">
                     </ComboBox>
                  </DataTemplate>
               </xctk:EditorTemplateDefinition.EditingTemplate>
            </xctk:EditorTemplateDefinition>
         </xctk:PropertyGrid.EditorDefinitions>
      </xctk:PropertyGrid>
   </Grid>
</Window>
May 1, 2014 at 10:22 PM
Thank you Boucher!

I had two issues with the provided code:
1.) For some reason, when we specify the combobox explicitly (inside the EditorTemplateDefinition) it wasn't pickup up our default style. I'm not sure if this is how the propertygrid works, or if it was due to a modification that we made locally. Anyway, I added a new style which based itself on our default combobox style so that we could call the style explicitly.

<Style x:Key="PropertyGridComboStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}"/>

2.) The next issue was that I needed a way to localize the strings. I wasn't able to do it using the xaml Array (shown above). Perhaps there is a way, but I couldn't find it. So, I defined my combobox now like this:

<xctk:EditorTemplateDefinition >
            <xctk:EditorTemplateDefinition.TargetProperties>
              <sys:String>SomeOption</sys:String>
            </xctk:EditorTemplateDefinition.TargetProperties>
            <xctk:EditorTemplateDefinition.EditingTemplate >
              <DataTemplate>
                <ComboBox  SelectedIndex="{Binding Value, Converter={converters:MyEnumToIntConverter}}" Style ="{DynamicResource PropertyGridComboStyle}">
                  <ComboBoxItem Content="{x:Static properties:Resources.MyLocalizedOption1}"/>
                  <ComboBoxItem Content="{x:Static properties:Resources.MyLocalizedOption2}"/>
                </ComboBox>
              </DataTemplate>
            </xctk:EditorTemplateDefinition.EditingTemplate>
          </xctk:EditorTemplateDefinition>
3.) Finally, I provided a converter to convert from my enum to an index.

Thank you very much for your help!
 -Glenn
Developer
May 2, 2014 at 12:42 PM
Hi Glenn,

1) I tried to use the style defined explicitly as you did and it worked. Here's my code, just setting the IsEditable property in the style :
 <Style x:Key="PropertyGridComboStyle"
             TargetType="ComboBox"
             BasedOn="{StaticResource {x:Type ComboBox}}">
         <Setter Property="IsEditable"
                 Value="True" />
</Style>

.....
<xctk:EditorTemplateDefinition.EditingTemplate>
                  <DataTemplate>
                     <ComboBox ItemsSource="{StaticResource comboSource1}"
                               Style="{DynamicResource PropertyGridComboStyle}">
                     </ComboBox>
                  </DataTemplate>
</xctk:EditorTemplateDefinition.EditingTemplate>
So I believe there Is something else with the comboBoxItem or internally.

2) and 3). I believe this is a working option.
May 2, 2014 at 12:43 PM
Ok, good to know. Thanks!