chincheta73
chincheta73

Reputation: 217

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

Upvotes: 4

Views: 1431

Answers (2)

jithu
jithu

Reputation: 2467

in app.xaml.cs

  public App()
        {            
          
            CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
            ci.DateTimeFormat.ShortDatePattern = "M/d/yyyy";
            Thread.CurrentThread.CurrentCulture = ci;
        }

Upvotes: 0

mm8
mm8

Reputation: 169200

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)));
    }
}

Upvotes: 2

Related Questions