views:

769

answers:

4

I succesfully created some constants in Silverlight's XAML like that

  <sys:Boolean x:Key="foo">True</sys:Boolean>

However, when I try to do the same with a DateTime (to initialize some DatePicker controls)

<sys:DateTime x:Name="myDate"/>

the system throws an XamlParseException "Unknown element: DateTime. [Line: xxx Position: xxx]"

Is there a way to declare DateTime constants (I'm thinking of DateTime.Now) in XAML?

A: 

In WPF you can do this with x:Static:

<FrameworkElement x:Key="dt" Tag="{x:Static s:DateTime.Now}" />

That's from Charles Petzold's all Xaml clock: http://www.charlespetzold.com/blog/2006/04/070132.html

Unfortunately there is no Static markup attribute in Silverlight so I'm not sure how you'd replicate this. Would really like to see an answer though!

James Cadd
cool. I was exploring that option but didn't think it would work.
scottmarlowe
A: 

i think you can set only specific dates :

> <telerikInput:RadDatePicker 
> SelectedDate="30/7/2009" />
Vanilla
+1  A: 

I'm not aware of any way to do it without writing code. You'll have to expose DateTime.Now as a property in your data model, and bind to it as usual.

Pavel Minaev
A: 

I can think of 2 Possible ways to do this:

1: (I didn't know you could do this until just now):

<x:Code>
    <![CDATA[
    private DateTime m_MyDateTime = DateTime.Now;
    ]]>
</x:Code>

2. You might try creating a CustomControl that inherits from FrameworkElement, and expose a DateTime DependencyProperty so you can DataBind to it.

EDIT:

I'm not 100% sure if either approach would work in Silverlight, I'm just offering a solution based on my experience with WPF Client Apps.

Pwninstein