Using the Xaml IValueConverter Generator

I have a generic class that implements IValueConverter . Something like:

 class MyValueConverter<T> : IValueConverter 

Since XAML 2009, I can use it as follows:

 <my:MyValueConverter x:TypeArguments='x:String'/> 

But, apparently, this did not allow "compiling" XAML (I think we will have to wait for .NET 5)

My current workaround subclasses it for each use:

 class FooMyValueConverter : MyValueConverter<Foo> 

Is it possible to do this in markup only with XAML 2006?

+7
source share
1 answer

Perhaps you can do this with MarkupExtension ( archive ) ( v4 ). Something like:

 public class MyMarkupExtension : MarkupExtension { public MyMarkupExtension() { this.Type = /* some default type */; } public MyMarkupExtension(Type type) { this.Type = type; } public Type Type { get; private set; } public override object ProvideValue(IServiceProvider serviceProvider) { Type type = typeof(MyValueConverter<>).MakeGenericType(this.Type); return Activator.CreateInstance(type); } } 

Then you will use it as {Binding ... Converter={local:MyMarkup {x:Type BounceEase}}}

+5
source

All Articles