Question

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?

Was it helpful?

Solution

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";

OTHER TIPS

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

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

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.

I notice this is an answered question.

But I would like to notify about this link to a Silverlight 5 control toolkit I have recently started creating. It contains (among other controls) a DateTimeBox control in which you can handle both date and time within the same control. At this point, it is still under development, but it should be usable for most scenarios.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top