Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IValueConverter get 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 argument culture of the Convert method is always "en-US", even when I change the culture of the Windows Phone device (or emulator) say to german Germany, the culture argument stays english.

like image 665
Hinek Avatar asked Aug 16 '11 15:08

Hinek


1 Answers

Not a bug, intended behaviour. See this post on MSConnect WPF Binding uses the wrong CurrentCulture by default.

The solution is to set the Language property of your PhoneApplicationPage to the CurrentCulture, like this:

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

Or alternatively specify the culture in XAML, using the Language attribute, like this:

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

Or on the PhoneApplicationPage it self

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

But a much better solution is not to have a value-converter that's depending on the culture argument.

Edit: I blogged about a alternative solution: DateTime formatting in a ValueConverter

like image 52
Claus Jørgensen Avatar answered Oct 09 '22 15:10

Claus Jørgensen