ConverterParameter-以任何方式传递某些分隔列表吗? [英] ConverterParameter -- Any way to pass in some delimited list?

查看:65
本文介绍了ConverterParameter-以任何方式传递某些分隔列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,如果我有:

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

您将如何传递某些类型的项数组作为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?

推荐答案

ConverterParameter 的类型为 object 意味着在解析XAML时不会进行任何隐式转换,如果您传递任何定界列表,它将仅被解释为字符串。当然,您可以将其拆分为convert方法本身。

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>

这篇关于ConverterParameter-以任何方式传递某些分隔列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆