StringFormat 和带标签的多重绑定
我想使用 StringFormat 来做这样的事情:
<Label x:Name="myLabel">
<Label.Content>
<Multibinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</Label.Content>
</Label>
但是,它不起作用,我收到了这个错误:
MultiBinding 失败,因为它没有有效的转换器。 MultiBindingExpression:目标元素是“Label”(Name=“myLabel”);目标属性是“内容”(类型“对象”)
有什么方法可以使此代码工作吗?
I would like to use StringFormat to do someting like this :
<Label x:Name="myLabel">
<Label.Content>
<Multibinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</Label.Content>
</Label>
However, it's doesn't work and I got this error instead :
MultiBinding failed because it has no valid Converter. MultiBindingExpression:target element is 'Label' (Name='myLabel'); target property is 'Content' (type 'Object')
Is there any way to make this code work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法绑定它,因为您试图将字符串绑定到一个无法工作的对象,因为 StringFormat 要求其目标是字符串类型。您可以通过使用 TextBlock(具有 Text 属性)或将 Textblock 作为 Label 的子项来解决此问题:
You cant bind this because you are trying to bind a string to an object which wont work because StringFormat requires its target to be a string type. You can get around this by either using a TextBlock instead (which has a Text property) or putting the Textblock as the child of the Label:
对于那些想知道的人,您还可以保留 Leom Burke 答案中的
标签。这又节省了两行代码。For those wondering you can also leave the
<Label.Content>
tag from Leom Burke's answer. This saves another two lines of code.其中
Resources.MyText
可以容纳诸如“Fox Jumps over {0}”之类的内容。Where
Resources.MyText
can hold anything like "Fox jumps over {0}."