Use IValueConverter with DynamicResource? Use IValueConverter with DynamicResource? wpf wpf

Use IValueConverter with DynamicResource?


I know i am really late to this but what definitely works is using a BindingProxy for the DynamicResource like this

<my:BindingProxy x:Key="someHeightProxy" Data="{DynamicResource someHeight}" />

Then applying the converter to the proxy

<RowDefinition Height="{Binding Source={StaticResource someHeightProxy}, Path=Data, Converter={StaticResource gridLengthConverter}}" />


Try something like that:

Markup extension:

public class DynamicResourceWithConverterExtension : DynamicResourceExtension{    public DynamicResourceWithConverterExtension()    {    }    public DynamicResourceWithConverterExtension(object resourceKey)            : base(resourceKey)    {    }    public IValueConverter Converter { get; set; }    public object ConverterParameter { get; set; }    public override object ProvideValue(IServiceProvider provider)    {        object value = base.ProvideValue(provider);        if (value != this && Converter != null)        {            Type targetType = null;            var target = (IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget));            if (target != null)            {                DependencyProperty targetDp = target.TargetProperty as DependencyProperty;                if (targetDp != null)                {                    targetType = targetDp.PropertyType;                }            }            if (targetType != null)                return Converter.Convert(value, targetType, ConverterParameter, CultureInfo.CurrentCulture);        }        return value;    }}

XAML:

<RowDefinition Height="{my:DynamicResourceWithConverter someHeight, Converter={StaticResource gridLengthConverter}}" />


@Thomas's post is very close, but as others have pointed out, it only executes at the time the MarkupExtension is executed.

Here's a solution that does true binding, doesn't require 'proxy' objects, and is written just like any other binding, except instead of a source and path, you give it a resource key...

How do you create a DynamicResourceBinding that supports Converters, StringFormat?