为全局独立于 CultureInfo 的所有 DateTime 对象设置 StringFormat - WPF
Setting StringFormat for all DateTime objects globally independent of CultureInfo - WPF
有没有办法在我的 WPF 应用程序中为所有 DateTime
对象全局设置 StringFormat
而独立于 CultureInfo
?
我在整个应用程序中使用绑定到此类对象,如下所示:
<DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
而且我想避免必须将 StringFormat
参数添加到每个绑定。
我试图像这样覆盖当前的文化 DateTimeFormat
参数:
public void SetDateTimeFormat()
{
culture = Thread.CurrentThread.CurrentUICulture;
var newCulture = new CultureInfo(culture.Name);
// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.LongDatePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.ShortTimePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.LongTimePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.FullDateTimePattern = "dd/MMM/YYYY";
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)));
var lang = System.Windows.Markup.XmlLanguage.GetLanguage(newCulture.IetfLanguageTag);
FrameworkContentElement.LanguageProperty.OverrideMetadata(
typeof(System.Windows.Documents.TextElement),
new FrameworkPropertyMetadata(lang)
);
}
但是不行
Is there any way to set the StringFormat
for all DateTime
objects globally independently of the CultureInfo
in my WPF Application
A DateTime
对象没有任何 StringFormat
但 Binding
有。您可以创建自定义 Binding
class:
public class MyCustomBinding : Binding
{
public MyCustomBinding(string path)
:base(path)
{
StringFormat = "yyyy-MM-dd";
}
}
用法:
<DataGridTextColumn Header="Date" Binding="{local:MyCustomBinding Date}"/>
或者您可以设置元素的 Language
属性:
<DataGrid x:Name="dg" Language="en">
您也可以对 App.xaml.cs
:
中的所有元素全局执行此操作
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var culture = new CultureInfo("en");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)));
}
}
在app.xaml.cs
public App()
{
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "M/d/yyyy";
Thread.CurrentThread.CurrentCulture = ci;
}
有没有办法在我的 WPF 应用程序中为所有 DateTime
对象全局设置 StringFormat
而独立于 CultureInfo
?
我在整个应用程序中使用绑定到此类对象,如下所示:
<DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
而且我想避免必须将 StringFormat
参数添加到每个绑定。
我试图像这样覆盖当前的文化 DateTimeFormat
参数:
public void SetDateTimeFormat()
{
culture = Thread.CurrentThread.CurrentUICulture;
var newCulture = new CultureInfo(culture.Name);
// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.LongDatePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.ShortTimePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.LongTimePattern = "dd/MMM/YYYY";
newCulture.DateTimeFormat.FullDateTimePattern = "dd/MMM/YYYY";
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)));
var lang = System.Windows.Markup.XmlLanguage.GetLanguage(newCulture.IetfLanguageTag);
FrameworkContentElement.LanguageProperty.OverrideMetadata(
typeof(System.Windows.Documents.TextElement),
new FrameworkPropertyMetadata(lang)
);
}
但是不行
Is there any way to set the
StringFormat
for allDateTime
objects globally independently of theCultureInfo
in my WPF Application
A DateTime
对象没有任何 StringFormat
但 Binding
有。您可以创建自定义 Binding
class:
public class MyCustomBinding : Binding
{
public MyCustomBinding(string path)
:base(path)
{
StringFormat = "yyyy-MM-dd";
}
}
用法:
<DataGridTextColumn Header="Date" Binding="{local:MyCustomBinding Date}"/>
或者您可以设置元素的 Language
属性:
<DataGrid x:Name="dg" Language="en">
您也可以对 App.xaml.cs
:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var culture = new CultureInfo("en");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)));
}
}
在app.xaml.cs
public App()
{
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "M/d/yyyy";
Thread.CurrentThread.CurrentCulture = ci;
}