IValueConverter gets the wrong culture in Windows Phone 7

I created a value converter in my Windows Phone 7 ...

public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // ... } // ... } 

... and use it like this:

 <TextBlock Text="{Binding SomeField, Converter={StaticResource MyConverter}, ConverterParameter=SomeParameter}" <!-- ... --> /> 

My problem: the culture of the argument method of the Convert method is always "en-US", even when I change the culture of the Windows Phone device (or emulator) to German Germany, the culture argument remains English.

+7
source share
3 answers

Not a mistake, the alleged behavior. See This post on MSConnect WPF Binding uses an invalid CurrentCulture by default .

The solution is to set the Language property of your PhoneApplicationPage to CurrentCulture, for example:

 Language = XmlLanguage.GetLanguage( Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName); 

Or alternatively specify the culture in XAML using the Language attribute, for example:

 <TextBlock Language="de-DE" Text="..." /> 

Or on PhoneApplicationPage he himself

 <phone:PhoneApplicationPage Language="de-DE" ... 

But a much better solution is the lack of a value converter that depends on the culture argument.

Edit: I wrote about an alternative solution: Formatting DateTime in ValueConverter

+6
source

Have you tried to find CurrentCulture ?

There may be a bug in WP7 where this is not passed.

+1
source

I had this problem.

I solved this using the following:

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return string.Format(culture, "{0:N}, value); } 

Use culture to transform transform control, but you must also make sure that you leave the value parameter as an object. Changing this type affects how string.Format interacts with it.

+1
source

All Articles