ConverterParameter — Any way to pass in some delimited list?

眉间皱痕 提交于 2019-12-23 18:50:40

问题


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?


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/7436156/converterparameter-any-way-to-pass-in-some-delimited-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!