简体   繁体   中英

ConverterParameter — Any way to pass in some delimited list?

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?

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, eg

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>

ConverterParameter is not a dependency property, so cannot be based on a binding

You could hardcode a value, such as an x-delimited list of parameters which you .Split(x) in your Converter, or you can use a MultiConverter which allows you to send multiple bound values to a Converter.

<!-- Not sure the exact syntax, but I'm fairly sure you have 
     to escape the commas -->
<TextBlock Text="{Binding MyValue, 
           Converter={StaticResource TransformedTextConverter},
           ConverterParameter={};@,@|}" />

Or

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource MyMultiConverter}">
            <Binding Path="MyValue" />
            <Binding Path="Parameters" />
        </MultiBinding>
    </TextBlock.Text>
<TextBlock>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM