WPF Data Binding Cheat Sheet Update - The Internationalization Fix

UPDATE: I wrote a more in dept post about getting Wpf to pick up regional settings and even update them on the fly

There is a new version of the WPF Data Binding Cheat Sheet, there is a small block of code you have to add to any WPF application in order to correctly format dates and numbers using data binding (I define “correct” as what the user would expect), read on for the full details and the required code for your copy-paste pleasure.

By default, when you use data binding and the target property is a string, WPF will format your value using the US English culture and net the user’s settings.

On the other hand, if you bind to an object property then the .net framework will do the conversion - and will do the right thing.

For example, those two lines:

<Label Content="{Binding Source={x:Static s:DateTime.Now}}"/>
<TextBlock Text="{Binding Source={x:Static s:DateTime.Now}}"/> 

Will produce the same text (but with different margin) on a US English machine, on machine with different date format the first line will give the correct result while the second line will keep using the US English settings.

To correct this (in my opinion very serious) bug you have to add the following code before showing any GUI:

FrameworkElement.LanguageProperty.OverrideMetadata
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(
        CultureInfo.CurrentCulture.IetfLanguageTag)));

The beginning of the application’s Startup event seems a good place.

If you use the default WPF project template add the bold line to your App.xaml:

<Application x:Class="MyProject.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   StartupUri="Window1.xaml"
   Startup="Application_Startup">
   <Application.Resources>

And in your App.xaml.cs file add:

private void Application_Startup(object sender, StartupEventArgs e)
{
   FrameworkElement.LanguageProperty.OverrideMetadata
       typeof(FrameworkElement),
       new FrameworkPropertyMetadata(
           XmlLanguage.GetLanguage(
           CultureInfo.CurrentCulture.IetfLanguageTag)));
}

Download The Cheat sheet Here

posted @ Sunday, February 22, 2009 5:15 PM

Comments on this entry:

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by Mario at 3/17/2009 5:01 PM

This does not seem to work if you customize your date time formats in regional settings. Do you know of anyway to honor the custom settings?

# Decimal Separator

Left by Jared at 3/1/2011 4:20 AM

Thanks. I had a problem with the decimal separator character in a Double. It is a period (.) in English, but in Spanish it is a comma (,).

Your solution works perfectly.

However, on this page, your solution is missing a parenthesis. You PDF has the parenthesis just this page doesn't.

Thanks again,

Jared Barneck

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by Vicente Maciel Jr at 8/6/2011 4:22 PM

Thank you so much! Googled it everywhere and only your solution did the job the right way!
Cheers from Brazil!

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by hkon at 8/25/2011 1:38 PM

Hello I use this cheat sheet all the time, today I came across an issue which I could not find in the cheat sheet.

I wanted to bind to an attached property of parent element. When setting up the path to the attached property I had to enclose the path inside parentheses as described here msdn.microsoft.com/en-us/library/ms752300.aspx

Would love to see it make it in the cheat sheet

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by Nir at 8/28/2011 10:04 AM

hkon - thanks, I'll add it to the cheatsheet whn I have a little bit of free time

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by Richard at 10/5/2011 6:30 PM

Unfortunately, this doesn't entirely work in .NET 4.0 - elements derived from FrameworkElement pick up the new language, but elements derived from FrameworkContentElement do not.

Just noticed this in my app, where data bound to a TextBlock uses en-GB, but the same data bound to a Run uses en-US.

There doesn't seem to be any way to apply this fix, since you can't override the meta-data for the Language property on the FrameworkContentElement.

# re: WPF Data Binding Cheat Sheet Update - The Internationalization Fix

Left by Jeremy Brayton at 11/6/2013 7:21 PM

I know this is 2013 but I had to comment and say thank you to Richard.

I have an application translated to Arabic but to follow the design, I have to translate digits in specific areas back to 0-9, not the actual Arabic equivalent.

This is in .NET 4 where I see no FrameworkElement.LanguageProperty.OverrideMetadata call *anywhere in the code* mind you, as if in 4.0 this is done for you.

The closest I got on my own was finding that I needed to set the TextBlock Language property to "en-US". Through the process, I found an oddity where only inspecting the element through Snoop or inspect the entire control in VS (versus just the Text property directly) that the value would show as I expected.

I was looking at what I thought was a framework bug and it's only extremely fortunate that the problem with FrameworkContentElement to you actually solves my problem.

I need to write a blog post to try to make the Google juice work in my favor because this was extremely hard to pinpoint. I've overlooked this post specifically numerous times because nothing in it applied yet somehow, today of all days, I scroll to the comment and presto: fixed.

A million thanks if you read this.

Your comment:



 (will not be displayed)


 
 
Please add 3 and 7 and type the answer here: