使用的StringFormat将字符串添加到一个WPF XAML绑定 [英] Use StringFormat to add a string to a WPF XAML binding

查看:2459
本文介绍了使用的StringFormat将字符串添加到一个WPF XAML绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF 4的应用程序,包含有一个单向结合为整数值一个TextBlock(在这种情况下,以摄氏度的温度下)。在XAML看起来是这样的:

I have a WPF 4 application that contains a TextBlock which has a one-way binding to an integer value (in this case, a temperature in degrees Celsius). The XAML looks like this:

<TextBlock x:Name="textBlockTemperature"><Run Text="{Binding CelsiusTemp, Mode=OneWay}"/></TextBlock>

这工作正常显示实际温度值,但我想格式化这个值,所以它包括℃,而不是仅仅在数(30℃,而不是仅仅30)。我一直在阅读有关的StringFormat和我见过几个通用例子是这样的:

This works fine for displaying the actual temperature value but I'd like to format this value so it includes °C instead of just the number (30°C instead of just 30). I've been reading about StringFormat and I've seen several generic examples like this:

// 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}}"/>



不幸的是,没有一个我见过有附加一个字符串为I'绑定值的例子想要做。我敢肯定,这一定是简单的东西,但我没有任何运气找到它。 ?谁能向我解释如何做到这一点。

Unfortunately, none of the examples I've seen have appended a string to the bound value as I'm trying to do. I'm sure it's got to be something simple but I'm not having any luck finding it. Can anyone explain to me how to do that?

推荐答案

您第一个例子是有效的,你需要的是什么:

Your first example is effectively what you need:

<TextBlock Text="{Binding CelsiusTemp, StringFormat={}{0}°C}" />

这篇关于使用的StringFormat将字符串添加到一个WPF XAML绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆