【问题标题】:Why WPF ignores my CultureInfo.CurrentCulture为什么 WPF 会忽略我的 CultureInfo.CurrentCulture
【发布时间】:2019-02-25 00:52:19
【问题描述】:

我开始弄乱 MySQL.Data.EntityFramework (8.0.15) 并且日期格式错误。

在我的文化 ('pl-PL') 中,日期格式是“YYYY-MM-DD”,但 DataGrid 默认以“MM/DD/YYYY”格式显示日期。我的 Windows 区域设置是正确的。

Console.WriteLine(CultureInfo.CurrentCulture); // shows pl-PL

我最初的绑定是这样的:

<!-- wrong date format -->
Binding="{Binding date}" 

在 Binding 中设置 ConverterCulture 有效:

<!-- correct date format -->
{Binding date, ConverterCulture='pl-PL', StringFormat=d}

但是我不想到处设置ConverterCulture

我的问题是:

我该如何解决这个问题?

【问题讨论】:

  • 检查 CurrentUICulture 属性。

标签: mysql wpf entity-framework binding cultureinfo


【解决方案1】:

尝试设置

Thread.CurrentThread.CurrentUICulture

也许,你需要看看

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
    XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

【讨论】:

  • FrameworkElement.LanguageProperty.OverrideMetadata(...) 帮助了。谢谢!
  • 我想补充一点,虽然FrameworkElement.LanguageProperty.OverrideMetadata(...) 是标准解决方案,但它仅适用于FrameworkElements,但不适用于FrameworkContentElements,如Run。如果您需要对它们使用 Binding,您可以创建一个继承自 Binding 的类,并在构造函数中将属性 ConverterCulture 设置为 CultureInfo.CurrentCulture。然后,您可以将自定义绑定与 FramewordContentElements 一起使用。
猜你喜欢
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2014-06-15
  • 2017-09-06
相关资源
最近更新 更多