WPF XAML Bindings and CurrentCulture Display

I see some invalid behavior from XAML docs when changing CurrentCulture. When I have elements like this in a window:

<Window x:Class="WpfLocalizationLocBaml.Test" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib" x:Name="wndTest" Title="Test" Height="300" Width="300"> <StackPanel> <TextBlock x:Name="lblCultureName" Text="{Binding Source={x:Static glob:CultureInfo.CurrentCulture}, Path=DisplayName}" /> <TextBlock x:Name="lblLocaleDateValue" Text="{Binding ElementName=wndTest, Path=TestDate}"/> <TextBlock x:Name="lblLocaleNumberValue" Text="{Binding ElementName=wndTest,Path=NumberValue,StringFormat=c}" /> </StackPanel> </Window> 

as well as MessageBox.Show (NumberValue.ToString ("c")); When the form starts, I see different results.

If I run the form with the default language, everything will be obvious. However, if I change the culture in the code or at startup, date and number bindings still show formatting in the USA. The displayed value of MessageBox.Show () appropriately reflects the current culture.

Question: Does WPF support CurrentCulture bindings? And if so, what exactly determines the culture that is used for bindings. In my case, this is explicitly en-US, but no matter what I set in my project as the default language, it always binds to en-US.

Any ideas appreciated ...

+4
wpf localization
Jun 13 '09 at 21:15
source share
2 answers

It turns out that WPF does not honor CurrentCulture by default in bindings and instead defaults to the xml: Lang parameter defined in the XAML or en-US document, if not specified. This is a rather weak behavior - I don’t know why you wouldn’t have the automatic formatting of the culture, applied like any other user interface technology, but ...

Fortunately, there is an easy workaround that can be applied in the document constructor or in the base class Window / UserControl:

  // MAKE SURE you set the language of the page explicitly or else // all number and date formatting occurs using this.Language = XmlLanguage.GetLanguage( CultureInfo.CurrentCulture.IetfLanguageTag); 

This blog post has more info:

http://www.west-wind.com/weblog/posts/796725.aspx

+18
Jun 14 '09 at 18:39
source share

It's also worth noting that the same thing happens in Silverlight with the same solution, with the exception of replacing the IetfLanguageTag for the name.

+1
Jun 02 '11 at 13:13
source share



All Articles