DateTime XAML WinRT

2019-08-05 04:46发布

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?

1条回答
聊天终结者
2楼-- · 2019-08-05 04:59

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.

查看更多
登录 后发表回答