19

DynamicResource拡張機能を使用するときにコンバーターを定義する方法はありますか?の行の何か

<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />

残念ながら、これは私に次の例外を与えます:

タイプ「Binding」の「Source」プロパティに「DynamicResourceExtension」を設定することはできません。'DynamicResourceExtension'は、DependencyObjectのDependencyPropertyにのみ設定できます。

4

4 に答える 4

23

私はこれに本当に遅れていることを知っていますが、間違いなく機能するのは、このようなDynamicResourceにBindingProxyを使用することです

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

次に、コンバーターをプロキシに適用します

<RowDefinition Height="{Binding Source={StaticResource someHeightProxy}, Path=Data, Converter={StaticResource gridLengthConverter}}" />
于 2014-06-15T06:59:01.457 に答える
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}}" />
于 2011-01-26T14:14:08.413 に答える
3

@Thomasの投稿は非常に近いですが、他の人が指摘しているように、MarkupExtensionが実行されたときにのみ実行されます。

これは、真のバインディングを行い、「プロキシ」オブジェクトを必要とせず、ソースとパスの代わりにリソースキーを与えることを除いて、他のバインディングと同じように記述されたソリューションです...

コンバーター、StringFormat をサポートする DynamicResourceBinding をどのように作成しますか?

于 2015-11-20T00:18:01.117 に答える
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

名前空間の終了

于 2015-03-25T12:02:11.043 に答える