Is it possible to supply a type converter for a static resource in WPF?
Asked Answered
F

2

12

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!

Fielder answered 4/3, 2010 at 19:38 Comment(0)
C
24

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.

Crozier answered 4/3, 2010 at 19:47 Comment(2)
What if you wanted to get the color components from a StatisResource which is a Color? (For instance, to change the opacity of the StaticResource color.) Doing the following doesn't seem to work: <Color A="#99" R="{Binding Source={StaticResource PhoneAccentColor}, Path=R}" G="{Binding Source={StaticResource PhoneAccentColor}, Path=G}" B="{Binding Source={StaticResource PhoneAccentColor}, Path=B}"/>Ductile
It doesn't work because you can only set a Binding on a DependencyProperty of a DependencyObject. Color is a struct. You could create your own color wrapper object which is a DepdendencyProperty, and exposes A,R,G,B, and Color properties that are themselves DPs. Changing any of the properties would update the Color property, and changing it would update all the others.Crozier
S
4

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;
    }
}
Saddlebag answered 4/3, 2010 at 19:56 Comment(1)
Oh! This is interesting too. Thanks Thomas!Fielder

© 2022 - 2024 — McMap. All rights reserved.