wpf 如何将带空格的字符串传递给转换器参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32471013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass string with spaces to converterParameter?
提问by SSgi88
My sample code is below.
我的示例代码如下。
I want to pass 'Go to linked item' to ConverterParameterbut 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?
我怎样才能做到这一点?
回答by CharithJ
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}}
回答by mark_h
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}"
回答by DOTNET Team
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 IMultiValueConverterinterface for doing this.
您将在 Converter 的 Convert 方法中同时获得 VALUE1 和 VALUE2。为此,您需要实现IMultiValueConverter接口。
For detailed explanation, just have a look at this
有关详细说明,只需看看这个

