问题描述
我想用的StringFormat来做些事情是这样的:
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失败,因为它没有有效转换。 MultiBindingExpression:目标元素是标签(名称='myLabel');目标属性是内容(类型'对象')
有什么办法使这个代码工作的?
Is there any way to make this code work ?
推荐答案
您不能绑定这一点,因为你想一个字符串,它不会工作的对象绑定,因为需要的StringFormat它的目标是一个字符串类型。您可以使用一个TextBlock代替(其中有一个Text属性),或者把文本块作为标签的孩子解决这个问题有两种方法:
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:
<Label x:Name="myLabel">
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>
这篇关于的StringFormat和Multibinding与标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!