2

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

JD.

JD.
  • 15,171
  • 21
  • 86
  • 159

2 Answers2

4

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
  • 1,629
  • 1
  • 24
  • 42
  • 1
    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. Jan 07 '10 at 16:16
  • 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 Jan 08 '10 at 05:40
0

Refer to the answer of this StackOverflow question: How to change date format in Silverlight DatePicker control?

Community
  • 1
  • 1
Luke Baulch
  • 3,626
  • 6
  • 36
  • 44