Questo semplice converter

   1: public class DateConverter : IValueConverter
   2: {
   3:     public object Parameter { get; set; }
   4:  
   5:     #region IValueConverter Members
   6:  
   7:     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   8:     {
   9:         if (value == null)
  10:         {
  11:             return value;
  12:         }
  13:         DateTime dt = (DateTime)value;
  14:         if (parameter != null)
  15:         {
  16:             return dt.ToString(parameter as string);
  17:         }
  18:         return dt.ToString();
  19:     }
  20:     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  21:     {
  22:         return value;
  23:     }
  24:     #endregion
  25: }

lo si può usare per foramattare le date in binding, ad esempio per visualizzare solo l’anno di una data

   1: <TextBlock Text="{Binding Data, Converter={StaticResource dateConverter}, ConverterParameter='yyyy', Mode=OneWay}" />

Technorati Tags:

Update : Precisazione dopo il commento, in Silverlight 4.0 o WPF conviene usare direttamente la StringFormat.