繁体   English   中英

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

[英]ConverterParameter — Any way to pass in some delimited list?

基本上,如果我有:

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

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

ConverterParameterobject类型,意味着解析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}

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不是依赖属性,因此不能基于绑定

您可以对一个值进行硬编码,例如您在转换器中的.Split(x)的x分隔参数列表,或者您可以使用MultiConverter ,它允许您向转换器发送多个绑定值。

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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