【问题标题】:ResourceDictionary as ContentProperty in a ValueConverterResourceDictionary 作为 ValueConverter 中的 ContentProperty
【发布时间】:2011-01-13 14:28:40
【问题描述】:

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

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>

当我启动应用程序时一切正常,转换器转换一个值的名称并通过其键从 ResourceDictionary 获取图标。但在我的设计器中,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 资源?

【问题讨论】:

  • 您是否尝试过使用&lt;local:IconConverter.Items&gt; 显式设置属性?您的转换器上没有ContentPropertyAttribute,我想知道它是如何工作的,也许它是否让设计者感到困惑。

标签: wpf xaml resourcedictionary valueconverter contentproperty


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 2012-07-15
    • 2023-03-18
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多