使用DynamicResource
扩展程序时,有没有办法定义转换器?
<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />
不幸的是,这给了我以下的重复:
'DynamicResourceExtension'不能 在类型的'Source'属性上设置 '捆绑'。一个 'DynamicResourceExtension'只能是 在一个DependencyProperty上设置 的DependencyObject。
答案 0 :(得分:15)
我知道我到目前为止已经很晚了,但绝对有效的是使用BindingProxy作为DynamicResource,就像这样
<my:BindingProxy x:Key="someHeightProxy" Data="{DynamicResource someHeight}" />
然后将转换器应用于代理
<RowDefinition Height="{Binding Source={StaticResource someHeightProxy}, Path=Data, Converter={StaticResource gridLengthConverter}}" />
答案 1 :(得分:4)
尝试类似的东西:
标记扩展程序:
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}}" />
答案 2 :(得分:1)
我喜欢mkoertgen的答案。
以下是适用于我的VB.NET中IValueConverter代理的改编示例。我的资源“VisibilityConverter”现在包含在DynamicResource中,并使用ConverterProxy“VisibilityConverterProxy”进行转发。
用法:
...
xmlns:binding="clr-namespace:Common.Utilities.ModelViewViewModelInfrastructure.Binding;assembly=Common"
...
<ResourceDictionary>
<binding:ConverterProxy x:Key="VisibilityConverterProxy" Data="{DynamicResource VisibilityConverter}" />
</ResourceDictionary>
...
Visibility="{Binding IsReadOnly, Converter={StaticResource VisibilityConverterProxy}}"
代码:
Imports System.Globalization
Namespace Utilities.ModelViewViewModelInfrastructure.Binding
''' <summary>
''' The ConverterProxy can be used to replace StaticResources with DynamicResources.
''' The replacement helps to test the xaml classes. See ToolView.xaml for an example
''' how to use this class.
''' </summary>
Public Class ConverterProxy
Inherits Freezable
Implements IValueConverter
#Region "ATTRIBUTES"
'Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
Public Shared ReadOnly DataProperty As DependencyProperty =
DependencyProperty.Register("Data", GetType(IValueConverter), GetType(ConverterProxy), New UIPropertyMetadata(Nothing))
''' <summary>
''' The IValueConverter the proxy redirects to
''' </summary>
Public Property Data As IValueConverter
Get
Return CType(GetValue(DataProperty), IValueConverter)
End Get
Set(value As IValueConverter)
SetValue(DataProperty, value)
End Set
End Property
#End Region
#Region "METHODS"
Protected Overrides Function CreateInstanceCore() As Freezable
Return New ConverterProxy()
End Function
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
Return Data.Convert(value, targetType, parameter, culture)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Data.ConvertBack(value, targetType, parameter, culture)
End Function
#End Region
End Class
结束命名空间
答案 3 :(得分:1)
@Thomas的帖子非常接近,但正如其他人所指出的那样,它只在执行MarkupExtension时执行。
这是一个真正的绑定解决方案,不需要'代理'对象,并且像任何其他绑定一样编写,除了源代码和路径,你给它一个资源键......
How do you create a DynamicResourceBinding that supports Converters, StringFormat?