4

I've a WPF4 Application.

There I bind an UI Control to a DateTimeOffSet value. For example a textblock

<TextBlock Text="{Binding Due.LocalDateTime,StringFormat='{}{0:dd MMM HH:mm}'}"/>

But I want a culture aware display of the value. So for example in germany there is a 24h represenation and in america a AM/PM representation.

What is the best / smartest way to realize this ? Do I need a covnerter ? or is there a way to represent this in a xaml ? Also the order of the date fields are different and so on...

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143

2 Answers2

3

There are a ton of workarounds for this questionable "by-design" behavior here:

http://connect.microsoft.com/VisualStudio/feedback/details/442569/wpf-binding-uses-the-wrong-currentculture-by-default

To exactly replicate the custom settings chosen by the user in the Regional Settings control panel applet is hard, but generic locale-specific settings is easy enough by setting the xml:lang attribute.

E.g., you can add something like this to your top-level window element, for example:

Language="{Binding Source={x:Static Globalization:CultureInfo.CurrentCulture}, Path=IetfLanguageTag,Mode=OneTime}"

Again, I must stress, that this isn't perfect, because any custom display preferences chosen by the user will be ignored.

mpursuit
  • 663
  • 6
  • 22
Mark
  • 1,784
  • 16
  • 26
  • ah thanks i didn't knew that the wpf uses the "wrong" regional settings. – Boas Enkler Feb 21 '12 at 18:53
  • @Mark In what way are "custom display preferences chosen by the user ignored" ? What kind of display preferences can be done by the user, that will be ignored by explictly setting the language property. Do you have an example? IMHO the display preferences done by the user are ignored when the language property stays with its default "en-us". – bitbonk Mar 06 '13 at 14:19
  • 2
    If you go into Control Panel's Region and Language dialog, as well as choosing a format by region, you can customize each date/time format, as well as a load of number format settings. Setting the language tag in XAML doesn't pay any attention to these settings. – Mark Mar 07 '13 at 14:48
0

For anyone who interested:

<Window x:Class="ViewFolderChanges"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ...
    xml:lang="el-GR" >

<TextBlock Text="{Binding ElementName=datefrom, Path=SelectedDate, StringFormat=ddd MM yyyy}" VerticalAlignment="Center" Padding="3,0" Margin="2,0,5,0"/>