views:

32

answers:

1

Hi,

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 !

A: 

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.

Curt Nichols
You're absolutely right, thanks !I was focused on the CLR property (MyProp{ get;set; }), but in the case of the attached DP, accessors are static methods instead of property.
JYL