bit*_*onk 16 wpf binding ivalueconverter dynamicresource
有没有办法在使用DynamicResource扩展时定义转换器?有些东西
<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我以下的重复:
无法在"绑定"类型的"Source"属性上设置"DynamicResourceExtension".'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上设置.
mko*_*gen 18
我知道我到目前为止已经很晚了,但绝对有效的是像这样使用动态资源的BindingProxy
<my:BindingProxy x:Key="someHeightProxy" Data="{DynamicResource someHeight}" />
Run Code Online (Sandbox Code Playgroud)
然后将转换器应用于代理
<RowDefinition Height="{Binding Source={StaticResource someHeightProxy}, Path=Data, Converter={StaticResource gridLengthConverter}}" />
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情:
标记扩展:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<RowDefinition Height="{my:DynamicResourceWithConverter someHeight, Converter={StaticResource gridLengthConverter}}" />
Run Code Online (Sandbox Code Playgroud)