Silverlight - SmartDatetoDateConverter

Silverlight - SmartDatetoDateConverter

Old forum URL: forums.lhotka.net/forums/t/7358.aspx


Jack posted on Wednesday, July 29, 2009

I ran into a bit of a surprise when I modified the text formatting of a certain SmartDate and it didn't work after databinding.  I had previously created a simple IValueConverter so that I could databind my SmartDate properties.

My mistake/oversight was that I was simply returning a new SmartDate( ) from the ConvertBack which was binded to a DatePicker control.  What happened of course is that the new SmartDate didn't have the updated text formatting.  While it wasn't a big deal to 'reset' the textFormat in the set accessor it would be a pain if I did that a lot.

Does anybody have a better implementation?  I don't know exactly about the internals of the IValueConverter but I didn't think that copying the FormatText to a local variable would work as I assume there is only one converter created per control.

Any ideas?  I know there is the option of databinding to the SmartDate.Text as well but if the 3rd party control take date then that makes more sense althought I suppose I could create a converter for that as well.

 public class SmartDateToDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || ((SmartDate)value).IsEmpty) return null;
            return ((SmartDate)value).Date;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //if (value == null) return new SmartDate(SmartDate.EmptyValue);
            return new SmartDate((DateTime?)value);
        }
    }

Jack replied on Wednesday, July 29, 2009

While I'm still interested if anybody has some ideas I did change my property set accessor to:

  _setupProgramStartDate.Date = value.Date;

instead of

  _setupProgramStartDate = value;
  _setupProgramStartDate.FormatString = "dddd, MMMM dd, yyyy"

At least that way I don't have to set the FormatString multiple times

Copyright (c) Marimer LLC