WPF默认DateTime格式 [英] WPF Default DateTime format
问题描述
在处理大型WPF应用程序时,DateTime格式存在一些问题,这是一个庞大的代码库,许多地方未指定区域性。
Working on a large WPF application and having some issues with the DateTime format, it's a huge codebase and many places do not specify the culture.
当用户在Windows 7或8上打开应用程序时,日期时间格式有所不同(Win 7使用斜杠,Win 8使用破折号)。
When the user opens the application on Windows 7 or 8 the datetime format is different (Win 7 uses slashes and Win 8 uses dashes).
我尝试在应用程序启动中将文化设置为 en-US(请参见下面的链接),但似乎不起作用?
在WPF应用程序中全局设置文化(en-IN)
I tried setting the Culture to "en-US" in the Application Startup (see below link) but it doesn't seem work? Setting Culture (en-IN) Globally in WPF App
如何将WPF应用程序设置为不使用用户计算机的区域性?
How do I set my WPF application to not use the culture of the user machine?
推荐答案
这是我在 OnStartup
中使用的内容。它与您链接到的SO问题不同,因为我使用 DefaultThreadCurrentCulture
,因为它为整个过程和所有启动的线程设置了默认值。
Here is what I use in OnStartup
. It's different than the SO question you linked to as I use DefaultThreadCurrentCulture
as it sets the default for the entire process and any threads launched.
DefaultThreadCurrentCulture
和 DefaultThreadCurrentUICulture
在.NET 4.5和以后...
DefaultThreadCurrentCulture
and DefaultThreadCurrentUICulture
are in .NET 4.5 and later...
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)));
这篇关于WPF默认DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!