简体   繁体   English

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

[英]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. 你将如何传递某种类型的项目作为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. ConverterParameterobject类型,意味着解析XAML时不会有任何隐式转换,如果传入任何分隔列表,它只会被解释为字符串。 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: 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: 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 ConverterParameter不是依赖属性,因此不能基于绑定

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. 您可以对一个值进行硬编码,例如您在转换器中的.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={};@,@|}" />

Or 要么

<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