Silverlight 中的默认字符串格式

发布于 2024-10-06 09:07:32 字数 518 浏览 0 评论 0原文

例如,我有一个 double,

double d = 4.323d;

我想将其显示在 Silverlight 4 应用程序上的 TextBlock 中,但显示应该是这样的:

4.32

而且我无法更改绑定上的 StringFormat。

例外的是,如果数字是这样的:

double d2 = 4d;

那么它应该显示

4,而不是 4.00

最糟糕的例外是它应该考虑当前的 UI 文化,这意味着如果应用程序部署在美国,则应使用 . 作为小数分隔符,而在欧洲则应使用a , (当然不是在英国,但你明白了...)

如果我能够更改 StringFormat,我可以将默认格式设置为 #.## ,但我想通过 CultureInfo 来做到这一点

I've got a double, for example

double d = 4.323d;

And I want to display it in a TextBlock on a Silverlight 4 application, but the display should be this:

4.32

And I cannot change the StringFormat on the binding whatsoever.

The exception is that if the number is this:

double d2 = 4d;

Then it should display

4, not 4.00.

And the worst exception is that it should take in account the current UI culture, which implies that if the app is deployed in the US it should use a . as a decimal seperator, and in Europe it should use a , (well not in the UK, but you get the point...)

I could set the defaultformat to #.## IF I were able to change the StringFormat, but I want to do it through CultureInfo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-10-13 09:07:33

我暂时假设您认为不能在绑定中使用 StringFormat,因为它不使用 CultureInfo。既然如此,请将这两个用法添加到后面的用户控制代码中:-

using System.Windows.Markup;
using System.Threading;

然后将其添加到其构造函数中:-

Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

现在,与 StringFormat=#.## 的绑定将使用适当的小数分隔符当前的文化。

I'm going to assume for the moment that you believe you can't use StringFormat in binding because it doesn't use CultureInfo. That being the case add these two usings to your user control code behind :-

using System.Windows.Markup;
using System.Threading;

and then add this to its constructor:-

Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

Now a binding with StringFormat=#.## will use the appropriate decimal separator for the current culture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文