WPF StringFormat = {0:C}显示为美元

Coe*_*esy 44 wpf binding currency regional string-formatting

为什么这行代码

<TextBlock Text="{Binding Net, StringFormat=c}"/>
Run Code Online (Sandbox Code Playgroud)

当我的所有区域设置都设置为UK时,将结果输出为$ xx.xx.我希望它输出为£xx​​.xx.有任何想法吗?我尝试过stringformat的不同变体,包括StringFormat = {} {0:C},但仍然得到相同的结果.

谢谢你的期待.

Mat*_*ton 70

我不确定这是否已在.NET 4中修复,但WPF在渲染货币或日期等内容时从未接受过当前的文化.我认为这是一次大规模的疏忽,但幸运的是很容易纠正.

在您的App类中:

protected override void OnStartup(StartupEventArgs e)
{
    FrameworkElement.LanguageProperty.OverrideMetadata(
        typeof(FrameworkElement),
        new FrameworkPropertyMetadata(
            XmlLanguage.GetLanguage(
            CultureInfo.CurrentCulture.IetfLanguageTag)));
    base.OnStartup(e);
 }
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此优秀帖子.

  • 这不会获取区域设置的自定义更改(即我使用德语,但使用合理的日期格式[ISO 8601]).这也有解决方法吗? (7认同)

Mic*_*per 30

我在主窗口中执行语言="en-GB",例如

<Window x:Class="AllocateWPF.Vouchers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="692" Width="1000" Language="en-GB">
Run Code Online (Sandbox Code Playgroud)


Mar*_*rko 18

什么对我
有用:1)在app.xaml覆盖OnStartup()并添加 -System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("et-EE");

2)在XAML @ Window级别定义 - xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"

3)在XAML中 - <TextBox Text="{Binding Path=Price, StringFormat='{}{0:C}', ConverterCulture={x:Static sysglb:CultureInfo.CurrentUICulture}}" />

这会正确选择任何自定义区域设置.虽然我在第一步中使用手动创建的CultureInfo,但我确信可以传递其中一种静态类型 - 例如.System.Globalization.CultureInfo.CurrentCulture(我还没有测试过它......)

  • 这确实解决了自定义设置问题。对于步骤1,我使用了“ = new CultureInfo(CultureInfo.CurrentCulture.IetfLanguageTag)”,而不是对其进行硬编码。 (2认同)