CurrentUICulture confusion

Mar 30, 2009 at 10:00 AM
Edited Mar 30, 2009 at 10:13 AM
Hi there,

I currently have a WPF datagrid which is autogenerated from datatable and works quite nicely. One of the columns is our own customised date class. Despite the fact that the ToString() methods are overloaded to return a short date string (which is configured to be en-GB format on the machine) the date always gets rendered in en-US format. Upon debugging, it appears that the System.Threading.Thread.CurrentThread.CurrentUICulture is causing the problem as this always seems to be set to en-US format even though the System.Threading.Thread.CurrentThread.CurrentCulture is en-GB.

I have tried to set the CurrentUICulture to be CurrentCulture on startup of the application as a test, but this doesn't seem to make any difference.

Does anyone have any ideas how to change the default for this value (or even advice as to whether I should be changing this!)

Thanks!
Mar 31, 2009 at 7:57 AM
Hi, I don't have the answer but I've noticed the same problem in the DataGrid with number formats and this is what I've found out so far.

If I use a StringFormat like this in the XAML: Binding="{Binding Sum, StringFormat=N2}"" I'll get en-US format.
If I use a converter like this: Binding="{Binding Sum, Converter={StaticResource PriceConverter}}" I'll get a format based on my current UI-culture (my converter uses decimal.ToString("N2") and no manipulation of the current UI-culture).

The same things applies if I create my DataGrid columns dynamic in code.

I've found that in the source-code for the toolkit the en-US culture is hard-coded in some places. I've tried to change this to another culture and rebuild the toolkit but without any luck.

/Jan
Mar 31, 2009 at 8:20 AM
I have got a little bit further. It seems that the IFormatProvider passed to my ToString() method is defaulted to en-US and this is not really linked to the CurrentUICulture. After quite a lot of head scratching, I have found that by putting a language definition at the top of my XAML file (xml:lang="en-GB") causes the correct format provider to be passed.

I'm not sure if this is a long term solution though, as I obviously want the format to adjust to the user's locale settings in the same way that my hosting WinForms application will.
Mar 31, 2009 at 8:32 AM
Great tip, thanks. I didn't like the idea of having to write a converter just to get around the problem. But I agree with you, it's not a long time solution. Let's hope it's fixed in comming versions.

/Jan
Coordinator
Mar 31, 2009 at 6:48 PM
Thank you for reporting this issue.  This does indeed sound like a bug, and I've filed a bug in our internal database to track this.  It will be fixed in an upcoming release.  Thanks!
Coordinator
Apr 28, 2009 at 6:16 PM
After conferring with our Data team, we've learned that the behavior you're seeing is by design.  Bindings pass a culture into the converters - the culture is either explicitly declared via Binding.ConverterCulture, or obtained from the target element's Language property (this is an inherited property that can be set by xml:Lang).   In this case, the converter you wrote is ignoring the culture that's being passed in, and instead calls an API (Decimal.ToString("N2")) that uses the CurrentUICulture.

 

Note that bindings do not use CurrentUICulture;  this is also by design.  To get that effect, you can set the Language property of the target element to the CurrentUICulture.

Hope that helps!  Thanks,
Samantha

Apr 29, 2009 at 6:11 AM
Thank you for informing me.
/Jan

Från: samanthamsft [notifications@codeplex.com]
Skickat: den 28 april 2009 20:16
Till: Ryninge Jan
Ämne: Re: CurrentUICulture confusion [wpf:51692]

From: samanthamsft

After conferring with our Data team, we've learned that the behavior you're seeing is by design. Bindings pass a culture into the converters - the culture is either explicitly declared via Binding.ConverterCulture, or obtained from the target element's Language property (this is an inherited property that can be set by xml:Lang). In this case, the converter you wrote is ignoring the culture that's being passed in, and instead calls an API (Decimal.ToString("N2")) that uses the CurrentUICulture.

Note that bindings do not use CurrentUICulture; this is also by design. To get that effect, you can set the Language property of the target element to the CurrentUICulture.

Hope that helps! Thanks,
Samantha