Podoba mi się odpowiedź mkoertgena.
Oto przystosowany przykład dla proxy IValueConverter w VB.NET, który działał dla mnie. Mój zasób "VisibilityConverter" jest teraz zawarty jako DynamicResource i przekazywany z użyciem konwertera "VisibilityConverterProxy".
Zastosowanie:
...
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}}"
Kod:
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
nazw Koniec
pojawia się następujący błąd kompilatora: 'Nieznana właściwość„Converter”dla typu„MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension” ' – bitbonk
dobry pomysł, ale to nie działa. Istnieje niedopasowanie: 'ProvideValue' jest wywoływane raz przez parser XAML i nie powinno niczego konwertować. Zamiast tego powinien dostarczyć właściwość dependency coś, co umożliwi konwersję. – jeromerg
Dlaczego nie używałeś podejścia @mkoertgen, biorąc pod uwagę, że on odnawia Twój artykuł? Czy jest jakaś wada? – Dzyann