ConverterParameter - 以任何方式传递一些分隔列表?

mic*_*ael 3 c# data-binding wpf xaml ivalueconverter

基本上,如果我有:

<TextBlock Text="{Binding MyValue, Converter={StaticResource TransformedTextConverter},
           ConverterParameter=?}" />
Run Code Online (Sandbox Code Playgroud)

你将如何传递某种类型的项目作为ConverterParameter.我想我可以传入某种类型的分隔列表,但我不确定要使用哪种类型的分隔符,或者是否有内置方法传递参数数组?

H.B*_*.B. 6

ConverterParameter类型是object,这意味着当XAML解析不会有任何隐式转换,如果你在任何分隔的列表,它只会被解释为字符串传递.你当然可以在转换方法本身中拆分它.

但是,由于您可能需要更复杂的对象,因此在处理静态值时可以执行两项操作:将对象数组创建为资源并引用它或使用元素语法在适当位置创建数组,例如

1:

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

... ConverterParameter={StaticResource params}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)