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

发布于 2024-08-31 05:31:35 字数 222 浏览 4 评论 0原文

当我所有的区域设置都设置为英国时,为什么这行代码

<TextBlock Text="{Binding Net, StringFormat=c}"/>

输出结果为$xx.xx。我希望它输出为 £xx.xx。有什么想法吗?我尝试了字符串格式的不同变体,包括 StringFormat={}{0:C} 但仍然得到相同的结果。

感谢您的关注。

Why does this line of code

<TextBlock Text="{Binding Net, StringFormat=c}"/>

Output the result as $xx.xx when all my regional settings are set to UK. I expect it to output it as £xx.xx. Any ideas? I have tried different variations of the stringformat including StringFormat={}{0:C} but still get the same result.

Thanks for looking.

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

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

发布评论

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

评论(3

坚持沉默 2024-09-07 05:31:35

我不确定这个问题是否已在 .NET 4 中得到修复,但 WPF 在呈现货币或日期等内容时从未采用当前文化。我认为这是一个巨大的疏忽,但幸运的是很容易纠正。

在您的应用程序类中:

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

请参阅 这篇优秀的文章 了解更多信息。

I'm not sure if this has been fixed in .NET 4, but WPF has never picked up the current culture when rendering things like currency or dates. It's something I consider a massive oversight, but thankfully is easily corrected.

In your App class:

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

See this excellent post for more information.

怼怹恏 2024-09-07 05:31:35

我在主窗口中执行 Language="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">

I do Language="en-GB" in the main window e.g.

<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">
满天都是小星星 2024-09-07 05:31:35

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

2) 在 XAML @ 窗口级别中定义 - < code>xmlns:sysglb="clr-namespace:System.Globalization; assembly=mscorlib"

3) 在 XAML 中 -

此操作正确选取任何自定义区域设置。尽管我在第一步中使用手动创建的CultureInfo,但我确信可以传入其中一个静态类型 - 例如。 System.Globalization.CultureInfo.CurrentCulture(不过我还没有测试过......)

What works for me:
1) In app.xaml override OnStartup() and add - System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("et-EE");

2) Define in XAML @ Window level - xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"

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

This correctly picks up any custom regional settings. Although I'm using a manually created CultureInfo in the first step, I'm sure it's possible to pass in one of the static types - eg. System.Globalization.CultureInfo.CurrentCulture (I haven't tested it though...)

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