是否可以为 WPF 中的静态资源提供类型转换器?

发布于 2024-08-24 06:30:49 字数 596 浏览 7 评论 0原文

我有一个 WPF 新手问题。

想象一下我的用户控件有一个像这样的命名空间声明:

xmlns:system="clr-namespace:System;assembly=mscorlib"

并且我有这样的用户控件资源:

<UserControl.Resources>
    <system:Int32 x:Key="Today">32</system:Int32>
</UserControl.Resources>

然后在我的用户控件中的某个地方我有这个:

<TextBlock Text="{StaticResource Today}"/>

这将导致错误,因为 Today 被定义为整数资源,但 Text 属性需要一个字符串。这个例子是人为的,但希望能说明问题。

问题是,如果无法使我的资源类型与属性类型完全匹配,是否有办法为我的资源提供转换器?类似于用于绑定的 IValueConverter 或类型转换器。

谢谢你!

I have a newbie WPF question.

Imagine my user control has a namespace declaration like this:

xmlns:system="clr-namespace:System;assembly=mscorlib"

And I have resources for the user control like this:

<UserControl.Resources>
    <system:Int32 x:Key="Today">32</system:Int32>
</UserControl.Resources>

And then somewhere in my user control I have this:

<TextBlock Text="{StaticResource Today}"/>

This will cause an error because Today is defined as a integer resource, but the Text property is expecting a string. This example is contrived, but hopefully illustrates the question.

The question is, short of making my resource type exactly match the property type, is there a way for me to provide a converter for my resources? Something like IValueConverter for bindings or a type converter.

Thank you!

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

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

发布评论

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

评论(2

过期以后 2024-08-31 06:30:49

如果您使用 Binding,这是可能的。这看起来有点奇怪,但这实际上是可行的:

<TextBlock Text="{Binding Source={StaticResource Today}}" />

这是因为 Binding 引擎内置了基本类型的类型转换。另外,通过使用 Binding,如果内置转换器不存在,您可以指定自己的转换器。

It is possible if you use a Binding. It seems a little weird, but this will actually work:

<TextBlock Text="{Binding Source={StaticResource Today}}" />

It is because the Binding engine has built-in type conversion for the basic types. Also, by using the Binding, if a built-in converter doesn't exist, you can specify your own.

﹂绝世的画 2024-08-31 06:30:49

安倍的回答应该适用于大多数情况。另一种选择是扩展 StaticResourceExtension 类:

public class MyStaticResourceExtension : StaticResourceExtension
{
    public IValueConverter Converter { get; set; }
    public object ConverterParameter { get; set; }

    public MyStaticResourceExtension()
    {
    }

    public MyStaticResourceExtension(object resourceKey)
        : base(resourceKey)
    {
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        object value = base.ProvideValue(serviceProvider);
        if (Converter != null)
        {
            Type targetType = typeof(object);
            IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (target != null)
            {
                DependencyProperty dp = target.TargetProperty as DependencyProperty;
                if (dp != null)
                {
                    targetType = dp.PropertyType;
                }
                else
                {
                    PropertyInfo pi = target.TargetProperty as PropertyInfo;
                    if (pi != null)
                    {
                        targetType = pi.PropertyType;
                    }
                }
            }
            value = Converter.Convert(value, targetType, ConverterParameter, CultureInfo.CurrentCulture);
        }
        return value;
    }
}

Abe's answer should work in most situations. Another option would be to extend the StaticResourceExtension class :

public class MyStaticResourceExtension : StaticResourceExtension
{
    public IValueConverter Converter { get; set; }
    public object ConverterParameter { get; set; }

    public MyStaticResourceExtension()
    {
    }

    public MyStaticResourceExtension(object resourceKey)
        : base(resourceKey)
    {
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        object value = base.ProvideValue(serviceProvider);
        if (Converter != null)
        {
            Type targetType = typeof(object);
            IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (target != null)
            {
                DependencyProperty dp = target.TargetProperty as DependencyProperty;
                if (dp != null)
                {
                    targetType = dp.PropertyType;
                }
                else
                {
                    PropertyInfo pi = target.TargetProperty as PropertyInfo;
                    if (pi != null)
                    {
                        targetType = pi.PropertyType;
                    }
                }
            }
            value = Converter.Convert(value, targetType, ConverterParameter, CultureInfo.CurrentCulture);
        }
        return value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文