Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 TypeConverterAttribute 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 !

like image 634
JYL Avatar asked Aug 27 '10 13:08

JYL


1 Answers

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.

like image 125
Curt Nichols Avatar answered Nov 03 '22 01:11

Curt Nichols