如何将带空格的字符串传递给converterParameter?

时间:2022-05-19 00:08:05

My sample code is below.

我的示例代码如下。

I want to pass 'Go to linked item' to ConverterParameter but I can't because the string has spaces.

我想将'转到链接项'传递给ConverterParameter,但我不能,因为字符串有空格。

Text="{Binding Value, 
        Source={x:Static local:Dictionary.Instance}, 
        Converter={StaticResource StringConverter}, 
        ConverterParameter=Go to linked item, Mode=OneWay}"

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


16  

Option 1

选项1

Text="{Binding Value, 
        Source={x:Static local:Dictionary.Instance}, 
        Converter={StaticResource StringConverter}, 
        ConverterParameter='Go to linked item', Mode=OneWay}"

Option 2

选项2

If you want to use this in multiple places add a string resource.

如果要在多个位置使用它,请添加字符串资源。

<sys:String x:Key="GoToLink">Go to linked item</sys:String>

And pass the resource key.

并传递资源键。

ConverterParameter={StaticResource ResourceKey=GoToLink}}

#2


6  

If your string has spaces then wrap it in single quotes, double quotes won't work; this is probably due to the fact that the entire text field is wrapped in double quotes and so using them again within the binding would incorrectly indicate closure.

如果你的字符串有空格然后用单引号括起来,双引号将不起作用;这可能是因为整个文本字段都用双引号括起来,因此在绑定中再次使用它们会错误地指示闭包。

Text="{Binding Value, 
    Source={x:Static local:Dictionary.Instance}, 
    Converter={StaticResource StringConverter}, 
    ConverterParameter='Go to linked item', Mode=OneWay}"

#3


-3  

I hope you purpose is to pass the string with spaces to your converter methods. I would suggest you to use MultiBinding. Please refer to following demo code :

我希望你的目的是将带有空格的字符串传递给转换器方法。我建议你使用MultiBinding。请参考以下演示代码:

<TextBox>
  <TextBox.Text>
     <MultiBinding Converter="{StaticResource CONVERTERKEY}" >
        <Binding Path="VALUE1" />
        <Binding Path="VALUE2" />
     </MultiBinding>
  </TextBox.Text>
</TextBox>

And you will get both VALUE1 and VALUE2 at your Converter's Convert method. You need to implement IMultiValueConverter interface for doing this.

您将在Converter的Convert方法中同时获得VALUE1和VALUE2。您需要实现IMultiValueConverter接口才能执行此操作。

For detailed explanation, just have a look at this

有关详细说明,请看一下

#1


16  

Option 1

选项1

Text="{Binding Value, 
        Source={x:Static local:Dictionary.Instance}, 
        Converter={StaticResource StringConverter}, 
        ConverterParameter='Go to linked item', Mode=OneWay}"

Option 2

选项2

If you want to use this in multiple places add a string resource.

如果要在多个位置使用它,请添加字符串资源。

<sys:String x:Key="GoToLink">Go to linked item</sys:String>

And pass the resource key.

并传递资源键。

ConverterParameter={StaticResource ResourceKey=GoToLink}}

#2


6  

If your string has spaces then wrap it in single quotes, double quotes won't work; this is probably due to the fact that the entire text field is wrapped in double quotes and so using them again within the binding would incorrectly indicate closure.

如果你的字符串有空格然后用单引号括起来,双引号将不起作用;这可能是因为整个文本字段都用双引号括起来,因此在绑定中再次使用它们会错误地指示闭包。

Text="{Binding Value, 
    Source={x:Static local:Dictionary.Instance}, 
    Converter={StaticResource StringConverter}, 
    ConverterParameter='Go to linked item', Mode=OneWay}"

#3


-3  

I hope you purpose is to pass the string with spaces to your converter methods. I would suggest you to use MultiBinding. Please refer to following demo code :

我希望你的目的是将带有空格的字符串传递给转换器方法。我建议你使用MultiBinding。请参考以下演示代码:

<TextBox>
  <TextBox.Text>
     <MultiBinding Converter="{StaticResource CONVERTERKEY}" >
        <Binding Path="VALUE1" />
        <Binding Path="VALUE2" />
     </MultiBinding>
  </TextBox.Text>
</TextBox>

And you will get both VALUE1 and VALUE2 at your Converter's Convert method. You need to implement IMultiValueConverter interface for doing this.

您将在Converter的Convert方法中同时获得VALUE1和VALUE2。您需要实现IMultiValueConverter接口才能执行此操作。

For detailed explanation, just have a look at this

有关详细说明,请看一下