开发者

How to set a TypeConverter on an attached dependency property in Silverlight?

开发者 https://www.devze.com 2023-01-13 03:26 出处:网络
My goal is to be able to write this in XAML: <Grid> <Rectangle Fill=\"AliceBlue\" myCore:MyTimePanel.BeginningDate=\"03/03/2010\"

My goal is to be able to write this in XAML:

<Grid>
    <Rectangle Fill="AliceBlue"
            myCore:MyTimePanel.BeginningDate="03/03/2010"
            />
</Grid>

Problem : Silverlight XAML can't parse a DateTime from a string. So at runtime I have the XamlParseException "can't create a DateTime from that string".

When I use a simple DependencyProperty, I simply add a TypeConverterAttr开发者_StackOverflow社区ibute on the getter/setter and it works. Like this (idea from here):

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

But with an attached DP, there is no getter/setter. What can I do to be able to write the string date in XAML ?

Thanks !


But attached properties have a Get accessor--have you tried putting the type converter on the Get accessor?

Sorry about the version-specific link, it's the one that contains the pertinent information. From that page:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消