locked
Binding a date/time value in XAML?

    Question

  • So, I'm having a devil of a time specifying a data binding for a DateTime value in XAML. Here's the context, and the errors I'm seeing. I've got a simple UserControl implementing a view of a date with some custom formatting, like you might see in a calendar app. Defined by DateControl.xaml, it looks like this: 
    There are a couple of custom value converters for formatting the day of week and month fields, and they're written so they should work with System.DateTime or System.DateTimeOffset (which I know Windows.Foundation.DateTime is projected to, thanks to other threads on here). 
    I can preview the data bindings in Visual Studio by including this snippet in DateControl.xaml:
    <UserControl.DataContext>
      <System:DateTime>2011-10-31</System:DateTime>
    </UserControl.DataContext>
    
     However, that results in an XmlParseException at runtime. 
    An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in application1.exe but was not handled in user code
    
    Additional information: The type 'DateTime' was not found. [Line: 13 Position: 25]
    
    I get the same exception if I replace DateTime with DateTimeOffset, or if I try to use Windows.Foundation.DateTime. It works fine if I leave out any data specification in the XAML and just bind DataContext in the code-behind, but that loses the ability to preview the control in the editor (using System:DateTime in XAML works fine in VisualStudio). 
    So I guess my question is -- is there any way to specify a data binding for a DateTime value (any of the varieties of DateTime class) in XAML?
    There doesn't appear to be, but maybe I'm missing something. Is this just a bug/limitation of the developer preview bits?


    • Edited by Adam Alpern Thursday, October 6, 2011 10:05 PM formatting
    Thursday, October 6, 2011 9:56 PM

All replies

  • I'm having a devil of a time with dates myself. Windows::Foundation::DateTime is essentially a struct with a single numeric value that counts the number of milliseconds since some date (I don't remember the date, perhaps 1/1/1900? ). It is not the System.DateTime that we grew to know and love. Something else that is confusing me is that the ::Now member exists in documentation but when I try to call it (it can be found in the documentation information on this page). All I'm trying to do is get the member populated with the current date and I'm having problems.
    Joel Ivory Johnson | http://www.j2i.net | Windows Phone Developer MVP
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.
    Saturday, October 8, 2011 7:23 PM