ConverterParameter -- Any way to pass in some delimited list?

michael picture michael · Sep 15, 2011 · Viewed 7.3k times · Source

Basically, if I have:

<TextBlock Text="{Binding MyValue, Converter={StaticResource TransformedTextConverter},
           ConverterParameter=?}" />

How would you go about passing in some type of array of items as the ConverterParameter. I figured I could pass in some type of delimited list, but I'm not sure what type of delimiter to use, or if there is a built-in way to pass in an array of parameters?

Answer

H.B. picture H.B. · Sep 15, 2011

The ConverterParameter is of type object that means when the XAML is parsed there will not be any implicit conversion, if you pass in any delimited list it will just be interpreted as a string. You could of course split that in the convert method itself.

But as you probably want more complex objects you can do two things when dealing with static values: Create the object array as resource and reference it or create the array in place using element syntax, e.g.

1:

<Window.Resources>
    <x:Array x:Key="params" Type="{x:Type ns:YourTypeHere}">
        <ns:YourTypeHere />
        <ns:YourTypeHere />
    </x:Array>
</Window.Resources>

... ConverterParameter={StaticResource params}

2:

<TextBlock>
    <TextBlock.Text>
        <Binding Path="MyValue" Converter="{StaticResource TransformedTextConverter}">
            <Binding.ConverterParameter>
                <x:Array Type="{x:Type ns:YourTypeHere}">
                    <ns:YourTypeHere />
                    <ns:YourTypeHere />
                </x:Array>
            </Binding.ConverterParameter>
        </Binding>
    </TextBlock.Text>
</TextBlock>