Silverlight 中的默认字符串格式
例如,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我暂时假设您认为不能在绑定中使用 StringFormat,因为它不使用
CultureInfo
。既然如此,请将这两个用法添加到后面的用户控制代码中:-然后将其添加到其构造函数中:-
现在,与
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 :-and then add this to its constructor:-
Now a binding with
StringFormat=#.##
will use the appropriate decimal separator for the current culture.