在大型 WPF 应用程序上工作并且 DateTime 格式存在一些问题,这是一个庞大的代码库,许多地方没有指定区域性。
当用户在 Windows 7 或 8 上打开应用程序时,日期时间格式不同(Win 7 使用斜杠,Win 8 使用破折号)。
我尝试在应用程序启动中将文化设置为“en-US”(参见下面的链接),但它似乎不起作用? Setting Culture (en-IN) Globally in WPF App
如何将我的 WPF 应用程序设置为不使用用户计算机的区域性?
最佳答案
这是我在 OnStartup
中使用的。它与我使用 DefaultThreadCurrentCulture
时链接到的 SO 问题不同,因为它为整个进程和启动的任何线程设置默认值。
DefaultThreadCurrentCulture
和 DefaultThreadCurrentUICulture
在 .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/