我可以让 MultiBinding 与 StringFormat 一起使用:
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1} (hired on {2:MMM dd, yyyy})">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
<Binding Path="HireDate"/>
</MultiBinding>
</TextBlock.Text>
但是单一绑定(bind)的正确语法是什么? 以下不起作用(尽管它似乎与 this example 的语法相同):
<TextBlock Text="{Binding Path=HiredDate, StringFormat='{MMM dd, yyyy}'}"/>
回答:
谢谢马特,我需要的是你的两个答案的组合,这很好用:
<TextBlock Text="{Binding Path=HiredDate,
StringFormat='Hired on {0:MMM dd, yyyy}'}"/>
最佳答案
您希望在示例中将大括号从格式字符串中删除,因为您没有将它们用作占位符(就像您在 String.Format() 中使用“{0}”一样)。
所以:
<TextBlock Text="{Binding Path=HiredDate, StringFormat='MMM dd, yyyy'}"/>
如果要在字符串内的某处引用占位符值,可以通过使用反斜杠转义花括号来实现:
<TextBlock Text="{Binding Path=HiredDate, StringFormat='Hired on \{0\}'}"/>
关于xaml - 将 StringFormat 与单个绑定(bind)一起使用的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1012174/