views:

7686

answers:

5

This works great:

<my:DatePicker IsTodayHighlighted="True" Width="200">
</my:DatePicker>

But I want to format the date, something like this:

<my:DatePicker IsTodayHighlighted="True" Width="200" Format="yyyy-mm-dd">
</my:DatePicker>

Anyone know the syntax for this?

A: 

The Silverlight DatePicker has a SelectedDateFormat Property on it, this may be what you are looking for.

+8  A: 

Hi,

Unfortunately the DatePicker control currently does not support free DateTime formats.

If this is something you're interested in seeing up support in future version of DatePicker, please create a codeplex feature request that suggests that. http://silverlight.codeplex.com/WorkItem/Create.aspx

Just to point out that the new Silverlight Toolkit March 2009 TimePicker & TimeUpDown controls do support a full range of globalization options. One of those include free DateTime formats. So it is just a matter of public interest on whether or not we port that ability back to DatePicker. Have a look at the format for TimePicker @ http://silverlight.codeplex.com/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%201#TimePicker

In the meanwhile, The best workaround is to either change the local culture or the format on the local culture.

    public App()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");

or change the format on the local culture.

        public App()
    {
        Thread.CurrentThread.CurrentCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
        Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "D/m/yyyy";
JustinAngel
Setting the current culture works great - and you only have to do it once throughout your application.
Calanus
+1  A: 

Dear punkcoder!

SelectedDateFormat is an enumaration. (long, short)

A: 

You should define a custom control template and edit the textbox of the datepicker control to format the text

Deep1
A: 

You could just hide the controls textbox (with a smaller width), expose you're own (optionally set the IsEnabled to false) and use an Element binding and Converter. If you're using MVVM, then set the DataContext to your ViewModel. I suppose another option would be to overwrite the DataTemplate to not include the text box and do the same idea.

<StackPanel Orientation="Horizontal" Height="22">
      <TextBox x:Name="textBox2" Width="106" Text="{Binding ElementName=datePicker2, Path=SelectedDate, Mode=TwoWay, Converter={StaticResource internationalDateTimeFormatConverter}}" />
      <controls:DatePicker x:Name="datePicker2" IsTabStop="False" SelectedDate="{Binding TargetDatePicker, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="23" HorizontalAlignment="Left" />
</StackPanel>

UPDATE: The TwoWay binding from the text box to the the date picker works well, but it doesn't update the ViewModel Property. So I'm going to set the IsEnabled=False and call it good.

Kevin