1

To convert Enums to Icons I use a value converter like that:

public class IconConverter : IValueConverter
{
    public ResourceDictionary Items { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        string key = Enum.GetName(value.GetType(), value);
        return Items[key];

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I use it in my XAML like this:

<UserControl.Resources>
    <local:IconConverter x:Key="IconConverter">
        <ResourceDictionary Source="/Leister.WPFControls;component/ButtonStyles.xaml" />
    </local:IconConverter>
</UserControl.Resources>

When I start the application all works fine, the Converter converts the Name of a value and gets the Icon from the ResourceDictionary by its key. But in my Designer, Visual Studio 2010 alwas complains:

The object of type System.Windows.ResourceDictionary" can not be cast to type "Microsoft.Expression.DesignModel.DocumentModel.DocumentNode".
at Microsoft.Expression.DesignModel.Core.InstanceBuilderOperations.SetValue(Object target, IProperty propertyKey, Object value)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)

This is anoying! Any idea? Is there a simpler solution to convert Enums to XAML-Icon Resources?

H.B.
  • 166,899
  • 29
  • 327
  • 400
falstaff
  • 3,413
  • 2
  • 25
  • 26
  • Have you tried explicitly setting the property using ``? Your converter doesn't have a `ContentPropertyAttribute` on it, and I'm wondering how this works at all, and perhaps whether it's confusing the designer. – Kent Boogaart Jan 13 '11 at 15:58

1 Answers1

1

I know its late but maybe the ContentPropertyAttribute would help the designer:

[ContentProperty("Items")]
public class IconConverter : IValueConverter
{
    public ResourceDictionary Items { get; set; }

http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute.aspx

Claudiu Mihaila
  • 1,322
  • 8
  • 17