ResourceDictionary 作为 ValueConverter 中的 ContentProperty

发布于 2024-10-12 04:19:23 字数 1693 浏览 8 评论 0原文

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

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)

这太烦人了!有什么想法吗?是否有更简单的解决方案将枚举转换为 XAML 图标资源?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-10-19 04:19:23

我知道已经晚了,但也许 ContentPropertyAttribute 会对设计者有所帮助:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文