开发者

ResourceDictionary as ContentProperty in a ValueConverter

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.Instance开发者_如何学GoBuilderOperations.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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜