wpf 更改绑定中的默认千位和小数分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15000191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:39:29  来源:igfitidea点击:

Changing the default thousand and decimal separator in a binding

c#wpfxamlstring-formatting

提问by Milo? Ran?elovi?

Let's say I have a number 1234567.89. The number is displayed in a WPF TextBlock. I am trying to apply StringFormatattribute to the Textproperty so that the number would be displayed like:

假设我有一个数字1234567.89。该数字显示在 WPF TextBlock 中。我正在尝试将StringFormat属性应用于该Text属性,以便数字显示如下:

1.234.567,89

As you can see, the thousand and decimal separators are inverted from the invariant culture specification.

如您所见,千位分隔符和小数分隔符与不变区域性规范相反。

I've tried setting numerous combinations for the StringFormat, but without success. This is the latest I came up with:

我已经尝试为 设置多种组合StringFormat,但没有成功。这是我最近想到的:

Text="{Binding SomeBinding, StringFormat={}{0:#'.'##0','00}}"

But the output isn't correct. Also, using N2or changing culture isn't an option. I would like to avoid converters if possible.

但输出不正确。此外,使用N2或改变文化也不是一种选择。如果可能,我想避免使用转换器。

So, is there a way to change the default separators through XAML?

那么,有没有办法通过 XAML 更改默认分隔符?

回答by Andrey Gordeev

You don't have to changethe culture. Just use String.Formatwith specified culture (de-DE should be fine):

你不必改变文化。只需使用String.Format指定的文化(de-DE 应该没问题):

string output = String.Format(new CultureInfo("de-DE"), "{0:N}", yourDoubleValue);

Output: 9.164,32

输出: 9.164,32

If you want to do it in XAML, you could try:

如果你想在 XAML 中做到这一点,你可以尝试:

Text="{Binding SomeBinding, StringFormat={}{0:N}, ConverterCulture=de-DE}"