Click here to Skip to main content
13,000,022 members (47,466 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 20 Aug 2012

WPF Default Binding Format Culture

, 20 Aug 2012
Rate this:
Please Sign up or sign in to vote.
WPF Default Binding Format Culture

WPF formats non-string objects to a string using a fixed/hardcoded culture (en-US) regardless of the current culture (as in Thread.CurrentThread.CurrentCulture / CultureInfo.CurrentCulture).

Suppose you have a simple DataContext (ViewModel) like so:

public class Data
    public DateTime Now
             return DateTime.Now;

    public string NowText
              return DateTime.Now.ToString();

Binding an instance of `Data` to a view, with two TextBlocks, each of which using ‘Now’ and ‘NowText’ respectively will yield different results if your CurrentCulture is NOT en-US.

Working on an internationalizable WPF app, I’ve found out this sad truth. I don’t fully understand why this happens but I’ve found a way to “fix it”.

Somewhere before any piece of UI is shown on the screen (for example in App.xaml.cs in ApplicationStartup), just place this little piece of code:

  new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

It sets for any FrameworkElement (TextBlock and Label are included) the formatting culture to the current culture. Of course, you might need to change it again if the current culture changes (for example, an on-the-fly UI language changes).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Andrei Ion Rînea
Software Developer (Senior) IBM, Business Analytics
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 940456016-May-13 2:27
memberMember 940456016-May-13 2:27 
GeneralMy vote of 4 Pin
Adam Wojnar6-Oct-12 9:03
memberAdam Wojnar6-Oct-12 9:03 
GeneralRe: My vote of 4 Pin
Andrei Rinea6-Oct-12 21:47
memberAndrei Rinea6-Oct-12 21:47 
QuestionAlmost... Pin
Richard Deeming31-Aug-12 2:54
memberRichard Deeming31-Aug-12 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 20 Aug 2012
Article Copyright 2012 by Andrei Ion Rînea
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid