c# - WPF 默认日期时间格式

标签 c# .net wpf datetime cultureinfo

在大型 WPF 应用程序上工作并且 DateTime 格式存在一些问题,这是一个庞大的代码库,许多地方没有指定区域性。

当用户在 Windows 7 或 8 上打开应用程序时,日期时间格式不同(Win 7 使用斜杠,Win 8 使用破折号)。

我尝试在应用程序启动中将文化设置为“en-US”(参见下面的链接),但它似乎不起作用? Setting Culture (en-IN) Globally in WPF App

如何将我的 WPF 应用程序设置为不使用用户计算机的区域性?

最佳答案

这是我在 OnStartup 中使用的。它与我使用 DefaultThreadCurrentCulture 时链接到的 SO 问题不同,因为它为整个进程和启动的任何线程设置默认值。

DefaultThreadCurrentCultureDefaultThreadCurrentUICulture 在 .NET 4.5 及更高版本中......

var newCulture = new CultureInfo(displayCulture);

// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";

// You cannot use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture if 
// you dont have .NET 4.5 or later. Just remove these two lines and pass the culture 
// to any new threads you create and set Thread.CurrentThread...
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;

Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;

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

关于c# - WPF 默认日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808184/

相关文章:

c# - 为什么我的正则表达式在 C# 中不起作用,但在其他文本编辑器中起作用?

c# - Linq-to-SQL 日期格式 : query does not work

c# - 寻找 WCF basicHttpBinding https 教程或示例

.net - C++/CLI 终结器和运算符

wpf - 将 WPF UserControl 绑定(bind)到 View 模型和隐藏代码

wpf - 添加项目后 DataGrid 刷新自身时索引 ArgumentOutOfRangeException

c# - 如何识别重复的应用程序 list 证书

c# - C# 中的 void 与 private void

javascript - 如何从javascript json接收WebAPI中的字典

c# - 如何将 Windows 窗体元素发送到 wpf 应用程序的后面?