Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / XAML

Passing dates in XAML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Oct 2009CPOL2 min read 30.5K   110   11   5
An article demonstrating how to pass date values in XAML as property values.

Introduction

What this article will show is how to pass DateTime values into properties in XAML.

Background

Recently, I had the need for an encapsulated, reusable 'About Box' control that I could use throughout a number of Silverlight applications. I then wrote a user control that would serve this purpose. The AboutBox displays application releases that are grouped by the application version, and a list of changes/updates included in that version. An example of this can be seen below:

About Box example

Although the use and implementation of the AboutBox user control is beyond the scope of this article, one of the features of the AboutBox was to enable the developer to specify a date for a product release item. I made provision for this by creating a DateTime property called Date in the VersionInformation class. However, I encountered a problem when trying to specify a value for this property in the XAML of the page consuming the AboutBox user control, as follows:

XML
<applesControls:AboutBox Margin="5">
    <applesControls:AboutBox.ItemsSource>
        <applesClasses:VersionInformation VersionNumber="1.0.0.0" Date="12 January 2009">
        <applesClasses:VersionInformation.Changes>
            <applesClasses:Change Item="First release." />
        </applesClasses:VersionInformation.Changes>
        </applesClasses:VersionInformation>
    </applesControls:AboutBox.ItemsSource>
</applesControls:AboutBox>

I realized that XAML natively does not know how to convert a string to a DateTime value, and setting the Date property as Date="12 January 2009" would result in an AG_E_UNKNOWN_ERROR XAML parser error when running the application.

Thankfully, there is a way to resolve this. The answer lies in TypeConverter.

Using the Code

The sample code has been written as a Visual Studio 2008 SP1 solution that contains three projects:

  1. 46ApplesAboutBox
  2. This is a Silverlight 3 application that consists of a MainPage.xaml to display the AboutBox user control.

  3. 46ApplesAboutBox.Common
  4. A Silverlight 3 Class Library that contains the following class declarations:

    • VersionInformation
    • DateTypeConverter
    • The AboutBox user control
  5. 46ApplesaboutBox.Web
  6. An ASP.NET web application that hosts the 46ApplesAboutBox Silverlight application.

To run the demo:

  • Extract the zip archive (46ApplesAboutBox.zip) containing the solution to disk.
  • Make 46ApplesaboutBox.Web the startup project.
  • Set 46ApplesAboutBoxTestPage.aspx as the startup page.

Writing a Custom TypeConverter

For us to pass in a DateTime value as a custom property in XAML, we need to write a TypeConverter. For the AboutBox project, I created a class called DateTypeConverter that inherits from and overrides methods in the TypeConverter class. One of the more interesting methods that were overridden is the ConvertFrom method.

C#
public override object ConvertFrom(ITypeDescriptorContext context, 
                       CultureInfo culture, object value)
{
    if (value.GetType() == typeof(string))
    {
        try
        { return DateTime.Parse(value.ToString()); }
        catch
        { throw new InvalidCastException(); }
    }
    return base.ConvertFrom(context, culture, value);
}

This method is responsible for converting the specified string representation of the date to the intended DateTime value.

One more thing remains. We need to tell the Date property in our VersionInformation class that we want to use a custom type converter that will perform the conversion from a string to a DateTime. This is accomplished by adding a TypeConverter attribute to the Date property, as follows:

C#
[TypeConverter(typeof(DateTypeConverter))]
public DateTime Date
{
    get { return (DateTime)GetValue(_dateProperty); }
    set { SetValue(_dateProperty, value); }
}

This will now allow us to pass in a "12 January 2009" string into the Date property, and it will be converted to and treated as a DateTime value.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLocalization problem Pin
Nicolas Dorier20-Oct-09 23:35
professionalNicolas Dorier20-Oct-09 23:35 
GeneralRe: Localization problem Pin
Wayne Delport21-Oct-09 0:17
Wayne Delport21-Oct-09 0:17 
GeneralQuite a long way of doing things... Pin
Yogesh Jagota20-Oct-09 3:07
Yogesh Jagota20-Oct-09 3:07 
GeneralRe: Quite a long way of doing things... Pin
Wayne Delport20-Oct-09 3:53
Wayne Delport20-Oct-09 3:53 
GeneralRe: Quite a long way of doing things... Pin
Yogesh Jagota20-Oct-09 5:27
Yogesh Jagota20-Oct-09 5:27 
Converters also have overheads (albeit very low), but nvm. Smile | :)

BTW I will love to know the other 6 ways. One of them I can think of is using a public string property for conversion and a private datetime to store the value. What are the others?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.