views:

252

answers:

2

When a ValueConverter is used as part of a binding, one of the parameters to the Convert function is a System.Globalization.CultureInfo object.

Can anyone tell me where this culture object gets its info from?

I have some code that formats a date based on that culture. When i access my silverlight control which is hosted on my machine, it formats the date correctly (using the d/MM/yyyy format, which is set as the short date format on my machine). When i access the same control hosted on a different server (from my client machine), the date is being formatted as MM/dd/yyyy hh:mm:ss - which is totally wrong. Coincidentally the regional settings on the server are set to the same as my client machine.

This is the code for my value converter:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is DateTime)
        {
            if (parameter != null && !string.IsNullOrEmpty(parameter.ToString()))
                return ((DateTime)value).ToString(parameter.ToString());
            else
                return ((DateTime)value).ToString(culture.DateTimeFormat.ShortDatePattern);
        }
        return value;
    }

basically, a specific format can be specified as the converter parameter, but if it isn't then the short date pattern of the culture object is used.


Edit: alright, i've worked out the issue - the version of the silverlight control on the server was an old version. Duh. The developer responsible (that would be me :) has been instructed to give himself a slap around the ears for his retarded mistake, and is currently considering having a beer with his lunch because it may enhance his c0d1ng sk1llz for the rest of the day.

Thanks to those who took the time to answer though :)

+1  A: 

Here's a brief article:

http://en.csharp-online.net/Localization_Like_the_Pros%E2%80%94CurrentCulture_and_CurrentUICulture

Todd
Cheers Todd - see my edit to the post. I've given you an upvote for the good link.
slugster
+1  A: 

Hm that's interesting indeed. I wouldn't expect the server to have anything to do with it since Silverlight is running entirely on the client in both cases. But I would run Fiddler and see if there's anything in the HTTP headers that specifies a locale or language. I am not an expert on HTTP/IIS so I don't know if this is typical or not, but if the server is specifying a locale, the browser may be using that as the default CurrentCulture.

But looking at Reflector, the answer to your question is that it uses CultureInfo.CurrentUICulture unless the Language property of the target element is specified in which case that is used instead. You can also set a ConverterCulture on the binding itself which appears to be the highest priority.

Josh Einstein
Thanks josh, the problem is resolved, see my edit on the post. The server shouldn't have any influence on the culture, hence my confusion (i had updated the wrong server this morning). I've marked yours as the answer as it is more technically correct according to the question :)
slugster