ResourceDictionary as ContentProperty in a ValueConverter

957 views Asked by At

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?

1

There are 1 answers

0
Claudiu Mihaila On

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