DateTime XAML WinRT [英] DateTime XAML WinRT
问题描述
将一个字符串传递给需要 DateTime
的控件的 DependencyProperty
似乎不允许: / p>
无法将文本值'00:00:00'分配给DateTime类型的属性StartTime
p>
只是我还是不应该这样做?我想的解决方法是提供一个IValueConverter将字符串转换为DateTime对象。对于安排程序/压缩程序,如控件,这有点烦人。
发现一些光?
TypeConverter
在WinRT中不可用,而平台似乎有许多UI类型的内置转换 - 这种隐式转换不是其中之一。你有几个选择。
- 如上所述 - 你可以使用值转换器
- 您可以确保您的
DateTime
属性绑定到DateTime
查看模型属性。 - 将属性定义为
String
,并在控件中进行转换 - 如果您通常使用XAML字符串初始化该属性。它也值得添加String到属性的名称,以表明它是一个字符串 - 例如。 'StartDateString'。 - 如果您希望使用
DateTime
和String
types - 您可以拥有两种类型的属性,并在内部同步它们,确保防止在属性更改处理程序中重入。 - 将属性声明为类型
对象
并检测要设置的值的类型是直接设置DateTime
值,从String
或其他类型(DateTimeOffset
,TimeSpan
,...?)或抛出不受支持的值。 / li>
不幸的是,直到Windows平台团队添加对 TypeConverter
属性的支持 - 有一个漂亮的解决方案。
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?
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.
- As you mentioned - you can use a value converter
- You can make sure your
DateTime
property is bound to aDateTime
view model property. - 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'. - If you would like to use your control both with
DateTime
andString
types - you could have properties of both types and synchronize them internally, making sure to prevent reentrancy in property change handlers. - Declare the property as type
Object
and detect what type the values being set are to either set aDateTime
value directly, convert fromString
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.
这篇关于DateTime XAML WinRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!