Is there any way to set the StringFormat
for all DateTime
objects globally independently of the CultureInfo
in my WPF Application?
I use bindings to this kind of objects from the whole application, like this:
<DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
and I want to avoid having to add the StringFormat
parameter to every binding.
I have tried to override the current culture DateTimeFormat
parameters like this:
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)
);
}
But it doesn't works