最佳答案
我有一个 WPF 4应用程序,其中包含一个 TextBlock,它具有一个单向绑定到一个整数值(在本例中是以摄氏度为单位的温度)。XAML 看起来像这样:
<TextBlock x:Name="textBlockTemperature">
<Run Text="{Binding CelsiusTemp, Mode=OneWay}"/></TextBlock>
这对于显示实际的温度值很有用,但是我想格式化这个值,这样它就包含了摄氏度而不仅仅是数字(30摄氏度而不仅仅是30)。我一直在阅读关于 StringFormat 的文章,我看到了一些这样的通用示例:
// format the bound value as a currency
<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" />
还有
// preface the bound value with a string and format it as a currency
<TextBlock Text="{Binding Amount, StringFormat=Amount: {0:C}}"/>
不幸的是,我所看到的示例中没有一个像我正在尝试的那样在绑定值后面添加字符串。我知道肯定很简单,但我找不到。有人能给我解释一下怎么做吗?