开发者

CultureInfo on a IValueConverter implementation

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.


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.


Here's a brief article:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜