Evgeniy Miroshnichenko
Evgeniy Miroshnichenko

Reputation: 1865

How to pass targetType parameter to the Convert() method of the IValueConverter instance in XAML

Converter method of the IValueConverter instance can get some parameters:

Convert(object value, Type targetType, object parameter, CultureInfo culture);

How to pass targetType parameter from XAML?

Upvotes: 1

Views: 1383

Answers (1)

James Coliz
James Coliz

Reputation: 80

You don't pass targetType, the framework does so for you, depending on what type it needs converted into.

Consider the StringFormatConverter from Template 10: https://github.com/Windows-XAML/Template10/wiki/Converters

<Page.Resources>
    <converters:StringFormatConverter x:Key="StrFormatConverter" />    
</Page.Resources>
...
<TextBlock Text="{Binding DateTimeValue, Converter={StaticResource StrFormatConverter}, ConverterParameter=\{0:D\}}" />

In this case, the system needs a String, so it will pass 'String' in as the targetType parameter.

You should confirm that the type its expecting is the type you're prepared to convert into.

Upvotes: 2

Related Questions