c# – 如何在Silverlight中的附加依赖项属性上设置TypeConverter?

c# – 如何在Silverlight中的附加依赖项属性上设置TypeConverter?,第1张

概述我的目标是能够在XAML中编写: <Grid> <Rectangle Fill="AliceBlue" myCore:MyTimePanel.BeginningDate="03/03/2010" /></Grid> 问题: Silverlight XAML无法从字符串中解析DateTime.所以在运行时我有XamlParseExceptio 我的目标是能够在XAML中编写:
<GrID>    <Rectangle Fill="AliceBlue"            myCore:MyTimePanel.BeginningDate="03/03/2010"            /></GrID>

问题:
Silverlight XAML无法从字符串中解析DateTime.所以在运行时我有XamlParseException“无法从该字符串创建DateTime”.

当我使用一个简单的DependencyProperty时,我只需在getter / setter上添加一个TypeConverterattribute即可.像这样(来自here的想法):

[TypeConverter(typeof(DateTimeTypeConverter))]public DateTime MyDate{    get { return (DateTime)GetValue(MyDateProperty); }    set { SetValue(MyDateProperty,value); }}

但是使用附加的DP,没有getter / setter.如何才能在XAML中编写字符串日期?

谢谢 !

解决方法 但附加属性有一个Get访问器 – 你试过 putting the type converter on the Get accessor吗?

对于特定于版本的链接感到抱歉,它是包含相关信息的链接.从那个页面:

3 . You can attribute a type-level TypeConverter on the type that serves as the value type. This enables string conversion of all values of the type. For more information,see TypeConverters and XAML.

4 . You can attribute a property-level TypeConverter on the Get accessor method. This enables string conversion of the attached property. Applying TypeConverterattribute to the Get accessor method rather than the Set accessor method may seem nonintuitive,but that is where XAML processors expect to find the type conversion information (if any) for an attached property. For more information,see TypeConverters and XAML.

总结

以上是内存溢出为你收集整理的c# – 如何在Silverlight中的附加依赖项属性上设置TypeConverter?全部内容,希望文章能够帮你解决c# – 如何在Silverlight中的附加依赖项属性上设置TypeConverter?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1242788.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存