IValueConverter get wrong culture in Windows Phone 7
Asked Answered
F

3

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.

Foretaste answered 16/8, 2011 at 15:13 Comment(0)
B
6

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

Bedouin answered 16/8, 2011 at 17:49 Comment(7)
Your first suggestion sounds good to me, should I place it in the constructor of my Page or in a event called later?Foretaste
In the constructor. Otherwise you risk it not being applied in a timely fashion.Marshmallow
Ok, but now culture argument is "de", not "de-DE", what results in an IndexOutOfRange exception when I use it in any .Format method (tried DateTime and String) ...Foretaste
Right. Try the Name property instead of TwoLetterISOLanguageName if you need sub-cultures as well. But again, I would strongly encourage not to have culture dependant code in a converter.Marshmallow
Ok, now it is working, thanks. If you encourage not having culture dependant code in a converter, how would you bind a TextBlock to a DateTime and make it format the output as a short date of the current culture? I'm just beginning with Silverlight and WP7 so I'm willing to learn here ...Foretaste
ValueConverters are meant to always convert the same way, so you could write a converter that takes the format as a parameter, and then bind the converterparameter to a (static) resource (binding). Or you could do the date-time formatting in your viewmodel (best approach in my opinion).Marshmallow
@Foretaste let us continue this discussion in chatMarshmallow
E
1

Have you tried looking up the CurrentCulture?

Might be a bug in WP7 where that isn't being passed in.

Evangelinaevangeline answered 16/8, 2011 at 15:42 Comment(0)
S
1

I've just had this problem.

I solved it 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 convert control the conversion, but you must also make sure you leave the value parameter as an object. Changing it's type affects how the string.Format interacts with it.

Schoenberg answered 17/8, 2011 at 8:10 Comment(2)
Sorry, but how does that solve the problem of culture containing the wrong value (en-US instead of the culture configured in the phone settings)?Foretaste
Ah - sorry. This might help. it describes this problem and a work-around: forums.create.msdn.com/forums/t/88151.aspxSchoenberg

© 2022 - 2024 — McMap. All rights reserved.