tags:

views:

371

answers:

1

Is there a way to force Silverlight to use the users locale settings when presenting dates in a datagrid?

JD.

+1  A: 

You could use a converter which looks at the System.Globalization.CultureInfo.CurrentCulture

public class SmartDateConverter : IValueConverter {            
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        DateTime date;
        culture = System.Globalization.CultureInfo.CurrentCulture;
        if (value != null && DateTime.TryParse(value.ToString(), out date))
        {
            string strDate = string.Empty;
            strDate = date.ToString(culture.DateTimeFormat.ShortDatePattern.ToString());
            return strDate;
        }
        return null;
    }
Stephen Price
Thanks Stephen. I will have to apply this converter to each grid column. Is there a way to set it up globally so that when it sees a data time field, it automatically applies the converter? (a bit like themes and styles).
JD
Hmm you might be able to apply the converter to the control a datatemplate and then apply the datatemplate to each of the datetime columns but i think the problem would be each binding would need to be different, and you apply the converter to the binding. if you can get the binding to not require a path it may work. ie {Binding, Converter={StaticResource SmartDateConverter}}
Stephen Price