1

In an application, I am trying to use a 24 hour date format, but running into a problem where my bindings in xaml are still reverting to displaying a 12 hour format

Before the UI is shown, I update the CultureInfo object with HH:mm and HH:mm:ss as the Short and Long Time formats.

Then, in the UI, I bind to a DateTime object with a FormatString in the binding

 <TextBlock Text="{Binding TimeTest, StringFormat=t}" />

My Expectation is that I'd see 17:33, but end up seeing 5:33 PM. A workaround we've found is to pass in the current culture, but would like to avoid having to do this on every binding that potentially uses time.

<TextBlock Text="{Binding Date, StringFormat=t, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" />
  • There is a similar discussion on SO already. [use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl][1] [1]: http://stackoverflow.com/questions/5831455/use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl – Walt Ritscher May 07 '13 at 17:48

3 Answers3

0

According to the documentation for the ConverterCulture property:

In XAML this [...] inherits the value from the root element (or any element) of the page.

That's not perfect, but that would surely be better than having to set it on every element, assuming it works.

James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • I'm not sure how you'd set it globally as it doesn't look to be settable in xaml. Was hoping you'd be able to do something like `` – Steve Van Treeck May 07 '13 at 15:29
  • Not sure. Thought you might be able to do it via the DataContext on a parent element. But your custom culture solution looks more promising. – James Holderness May 07 '13 at 15:41
0

The problem, as you have found, is that WPF does culture information and localization in a different way than other parts of .NET.

There is a good discussion of the problem here on SO.

The WPFGlue blog has some further details

Community
  • 1
  • 1
Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
0

You can derive a class from Binding which presets the ConverterCulture, and use this instead of the original binding, like

    Public Class Binding
        Inherits System.Windows.Data.Binding

        Public Sub New()
            MyBase.New()
            ConverterCulture = System.Globalization.CultureInfo.CurrentCulture
        End Sub
        Public Sub New(ByVal path As String)
            MyBase.New(path)
            ConverterCulture = System.Globalization.CultureInfo.CurrentCulture
       End Sub
    End Class

and

<TextBlock Text="{my:Binding TimeTest, StringFormat=t}" />

You might also try a custom StringFormat, like StringFormat='HH:mm'

hbarck
  • 2,934
  • 13
  • 16
  • Looking at all of the different solutions, its a bit odd that there's no really simple way to do this when you'd expect that it would just work. In the end, I think we'll end up going this way or doing a custom `IValueConverter`+`MarkupExtension` that handles the display/parsing as necessary. – Steve Van Treeck May 07 '13 at 23:03