Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting StringFormat for all DateTime objects globally independent of CultureInfo - WPF

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

like image 643
chincheta73 Avatar asked Sep 14 '25 01:09

chincheta73


1 Answers

Is there any way to set the StringFormat for all DateTime objects globally independently of the CultureInfo in my WPF Application

A DateTime object doesn't have any StringFormat but a Binding has. You could create a custom Binding class:

public class MyCustomBinding : Binding
{
    public MyCustomBinding(string path)
        :base(path)
    {
        StringFormat = "yyyy-MM-dd";
    }
}

Usage:

<DataGridTextColumn Header="Date" Binding="{local:MyCustomBinding Date}"/>

Or you could set the Language property of the element:

<DataGrid x:Name="dg" Language="en">

You could also do this globally for all elements in your 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)));
    }
}
like image 123
mm8 Avatar answered Sep 16 '25 16:09

mm8