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

PropertyGrid: no useful default editor for List<enum>

Jan 7, 2014 at 12:03 PM
When using the property grid with an object containing a property of type "List<enum>", the associated editor for this property is the CollectionEditor. However, this makes little sense, as members of enums cannot be created like objects. Thus it is not possible to populate this list. Also viewing is of little help, since the current members are only displayed using their type names (instead of object.ToString()).
On the other hand, List<> of primitive types can very well be edited using a drop-down list. What about handling List<enum> the same way?
There are 2 little changes to the source code to accomplish this:

PrimitiveTypeCollectionControl.cs:
private IList ComputeItems()
{
...
object value = null;
try {
  if (ItemType.IsEnum) {
      value = Enum.Parse(ItemType, valueString);
  }
  else {
    value = Convert.ChangeType( valueString, ItemType );
  }
PropertyGridUtilities.cs:
internal static ITypeEditor CreateDefaultEditor( Type propertyType, TypeConverter typeConverter )
{
...
if( !listType.IsPrimitive && !listType.Equals( typeof( String ) ) && !listType.IsEnum )
editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.CollectionEditor();
else
editor = new Xceed.Wpf.Toolkit.PropertyGrid.Editors.PrimitiveTypeCollectionEditor();

Developer
Jan 14, 2014 at 6:09 PM