1

We are trying to allow the app to have its culture set at runtime and optionally override the way dates are displayed.

We create our own culture with:

private CultureInfo CreateCulture()
    {
        // start with basic culture object
        CultureInfo culture = Sys.Workspace.ServerProperties.UICulture ?? new CultureInfo(Properties.Settings.Default.DefaultCulture);

        // override data format
        var dateFmt = Sys.Workspace.ServerProperties.DateFormatOverride;
        if (!string.IsNullOrEmpty(dateFmt))
        {
            culture.DateTimeFormat.ShortDatePattern = dateFmt;
            culture.DateTimeFormat.LongDatePattern = dateFmt;
            if (dateFmt.Contains("/")) culture.DateTimeFormat.DateSeparator = "/";
            if (dateFmt.Contains("-")) culture.DateTimeFormat.DateSeparator = "-";
        }

        return culture;
    }

Then in the initialization of the app we do:

        var culture = CreateCulture();

        // set ui culture
        Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = culture;

        // set WPF culture
        LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)));

We set the "Sys.Workspace.ServerProperties.DateFormatOverride" value to be "dd-MMM-yyyy". This doesn't seem to be respected by the bindings though. For example:

  <TextBlock TextAlignment="Center" HorizontalAlignment="Stretch" 
      Text="{Binding NoteDate, StringFormat={}{0:d}}" />

The NoteDate is a DataColumn with type of DateTime but displays the default cultures shortdate pattern.

If I set the main culture to en-GB I will get dd-mm-yyyy, if I set it to en-US I will get mm-dd-yyyy.

When I look at the Thread.CurrentThread.CurrentCulture and currentUICulture it is set correctly, i.e. it is en-gb and the shortdatepattern and longdatepattern are dd-MMM-yyyy but the dates do not display in this format.

1 Answer 1

1

The reason is the FrameworkElement's don't look at the Thread.CurrentThread.CurrentCulture for globalisation info.

Instead you have to define the culture manually for XAML elements...

Answered here

Sign up to request clarification or add additional context in comments.

2 Comments

darn you MS, thats just nasty. in the end I put a datetimeconverter in for each binding so that at least it was only done at one point.
But LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag))); covers that, doesn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.