1

Passing a string to the DependencyProperty of a control that takes a DateTime does not seem to be allowed:

Cannot assign text value '00:00:00' into property 'StartTime' of type 'DateTime'

Is it just me or shouldn't this be possible? The workaround I suppose is to provide a IValueConverter to convert strings to DateTime objects. For Scheduler/Calender like controls this is a little annoying.

Shed some light?

rtlayzell
  • 608
  • 4
  • 20
  • `DateTime time = "00:00:00";` is invalid: `Cannot implicitly convert type 'string' to 'System.DateTime'`. So it makes perfect sense, to me at least. – Bart van Nierop Feb 26 '14 at 12:56
  • Jap makes perfect sense, `DateTime time = Covert.ToDateTime("00:00:00");` would help in the conversion problem from code, it will give an object with todays date but with the time initialized like specified. If you want to do it with bindings a value converter is needed. – Spook Kruger Feb 26 '14 at 13:13
  • I was expecting a `DateTime` with the date portion uninitialized, this sort of thing works in WPF. WinRT on the other hand refuses to compile it out right. I would need a converter if I was binding to a string (not that I could see any plausible reason to do so) what seems to missing is a `TypeConverter/Attribute` on the `DateTime` type for WinRT. – rtlayzell Feb 26 '14 at 13:37

1 Answers1

1

TypeConverter isn't available in WinRT and while the platform seems to have some built in conversions for many UI types - this implicit conversion is not one of those. You have a few options though.

  1. As you mentioned - you can use a value converter
  2. You can make sure your DateTime property is bound to a DateTime view model property.
  3. Define your property as type String and do the conversions inside of your control - if you would typically initialize that property with a XAML string. It would also be worth it to append 'String' to the name of the property to make it clear that it's a string - e.g. 'StartDateString'.
  4. If you would like to use your control both with DateTime and String types - you could have properties of both types and synchronize them internally, making sure to prevent reentrancy in property change handlers.
  5. Declare the property as type Object and detect what type the values being set are to either set a DateTime value directly, convert from String or other types (DateTimeOffset, TimeSpan, ...?) or throw for unsupported values.

Unfortunately until the Windows platform teams add support for TypeConverter attribute - you don't have a pretty solution.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100