我有一个WPF 4应用程序,其中包含一个TextBlock,它具有一个单向绑定到一个整数值(在这种情况下,温度为摄氏度).XAML看起来像这样:
<TextBlock x:Name="textBlockTemperature">
<Run Text="{Binding CelsiusTemp, Mode=OneWay}"/></TextBlock>
Run Code Online (Sandbox Code Playgroud)
这适用于显示实际温度值,但我想格式化这个值,因此它包括°C而不是数字(30°C而不是30°).我一直在阅读有关StringFormat的文章,我看过几个这样的通用例子:
// format the bound value as a currency
<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" />
Run Code Online (Sandbox Code Playgroud)
和
// preface the bound value with a string and format it as a currency
<TextBlock Text="{Binding Amount, StringFormat=Amount: {0:C}}"/>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我所看到的所有例子都没有在我试图做的事情中附加一个字符串到绑定值.我敢肯定它必须简单但我找不到任何运气.任何人都可以向我解释如何做到这一点?
在数据绑定中,您可以使用多重绑定..对于多重绑定,您可以组合{} {0} {1}等属性.我的问题是第一个{}是什么意思?我不是在讨论用于选择要使用的属性的{0}.
谢谢.
如何对单个TextBlock进行数据绑定以说"嗨,Jeremiah"?
<TextBlock Text="Hi, {Binding Name, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)
寻找一个优雅的解决方案.有什么?我正试图远离为每个前缀/后缀组合编写转换器.