CultureInfo on a IValueConverter implementation
Posted
by slugster
on Stack Overflow
See other posts from Stack Overflow
or by slugster
Published on 2010-03-18T21:44:30Z
Indexed on
2010/03/18
22:01 UTC
Read the original article
Hit count: 877
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.
© Stack Overflow or respective owner