Xaml 使用单绑定的StringFormat的正确语法是什么?

Xaml 使用单绑定的StringFormat的正确语法是什么?,xaml,binding,Xaml,Binding,我可以使用多重绑定来处理StringFormat: <TextBlock.Text> <MultiBinding StringFormat="{}{0} {1} (hired on {2:MMM dd, yyyy})"> <Binding Path="FirstName"/> <Binding Path="LastName"/> <Binding Path="HireDate"/>

我可以使用多重绑定来处理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>

但单绑定的正确语法是什么?以下语法不起作用(尽管它似乎与以下语法相同):


答复: 谢谢Matt,我需要的是你的两个答案的组合,这非常有效:

<TextBlock Text="{Binding Path=HiredDate, 
    StringFormat='Hired on {0:MMM dd, yyyy}'}"/>

在示例中,您希望将大括号保留在格式字符串之外,因为您没有将它们用作占位符(就像在string.format()中使用“{0}”)

因此:


如果要在字符串中的某个位置引用占位符值,可以使用反斜杠转义大括号:

<TextBlock Text="{Binding Path=HiredDate, StringFormat='Hired on \{0\}'}"/>


您认为第二个示例不需要反斜杠,它在没有反斜杠的情况下工作,或者是因为其他原因。奇怪。在过去,当我忽略了反斜杠时,我有过编译错误。很高兴它对你有用!我发现,如果字符串以大括号开头,如“{0}Some Text”,则需要转义大括号。如果任何其他字符(如空格)先出现,则无需转义它们。
<TextBlock Text="{Binding Path=HiredDate, StringFormat='MMM dd, yyyy'}"/>
<TextBlock Text="{Binding Path=HiredDate, StringFormat='Hired on \{0\}'}"/>