ResourceDictionary作为ValueConverter中的ContentProperty

时间:2011-01-13 14:28:40

标签: wpf xaml resourcedictionary valueconverter contentproperty

要将枚举转换为图标,我使用类似的值转换器:

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();
    }
}

我在我的XAML中使用它:

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

当我启动应用程序一切正常时,Converter转换值的名称并通过其键从ResourceDictionary获取Icon。但在我的设计师中,Visual Studio 2010也抱怨道:

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)

这很烦人!任何的想法?有没有更简单的解决方案将Enums转换为XAML-Icon Resources?

1 个答案:

答案 0 :(得分:1)

我知道它的晚期,但ContentPropertyAttribute可能会帮助设计师:

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

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