#765 – WPF Data Binding Ignores CurrentCulture

Using the ToString method on numeric and date-based types from within your code, the output will reflect the current culture, as stored in the CurrentCulture property of the current thread.

When the data binding mechanism in WPF converts numeric or date-based data to a string, however, it doesn’t automatically use the current culture.  Instead, you must set the Language property of the FrameworkElement where the binding is occurring.  You typically set this property based on the CurrentCulture property of the current thread.  You can set the Language property of the topmost user-interface element, so that all of the other elements inherit the value.

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
        }

We can now do binding as follows (where BoundDate property is of type DateTime):

        <TextBlock Text="{Binding BoundDate, StringFormat=d}"/>

Now if we run with the region set to French (France), we see:
765-001

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

5 Responses to #765 – WPF Data Binding Ignores CurrentCulture

  1. It might be of interest to know that there is an unfortunate difference between XmlLanguge and CultureInfo. The XmlLanguage setting will NOT take into account any customization that have been made to the Regional settings in Windows. You will always get the standard settings for the specified region. CultureInfo.CurrentCulture handles this correctly though.

    • Sean says:

      Right–this is happening because you’re not pointing the Language property back to a full CultureInfo object that includes the customizations, but you’re just giving it a language tag. You can use the ConvertCulture property on the binding to point back to the actual CultureInfo object. This is clumsy, but works. See http://wpfglue.wordpress.com/2010/01/14/localized-value-formatting-in-wpf/

      You could also just make it a habit of always binding to locale-specific strings, i.e. strings that have already been converted based on the current culture. This is probably the more common approach in production code that is localized.

  2. Pingback: #766 – WPF Data Binding Ignores Custom Formatting | 2,000 Things You Should Know About WPF

  3. Pingback: Dew Drop – February 28, 2013 (#1,506) | Alvin Ashcraft's Morning Dew

  4. Pingback: #1,115 – Internationalization I – Obey CurrentCulture for Output | 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: