2011-01-26 81 views
12

有沒有辦法在使用DynamicResource擴展名時定義一個轉換器?東西的使用IValueConverter和DynamicResource?

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

線不幸給了我下面的錯誤時拋出:

A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.

回答

3

嘗試類似的東西:

標記擴展:

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}}" /> 
+0

我得到以下編譯器錯誤:'未知屬性「轉換器」類型「MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension」 ` – bitbonk 2011-04-14 18:13:13

+0

好主意,但它不工作。有一個不匹配:`ProvideValue`被XAML解析器調用一次,不應該轉換任何東西。相反,它應該提供依賴項屬性以啓用轉換。 – jeromerg 2014-06-11 11:08:17

12

我知道我真的來不及這一點,但使用BindingProxy像這樣

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

的DynamicResource然後應用轉換到代理

<RowDefinition Height="{Binding Source={StaticResource someHeightProxy}, Path=Data, Converter={StaticResource gridLengthConverter}}" /> 
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 

末命名空間

相關問題