Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the string format of the WPF DatePicker

I need to change the string format of the DatePickerTextBox in the WPF Toolkit DatePicker, to use hyphens instead of slashes for the seperators.

Is there a way to override this default culture or the display string format?

01-01-2010 
like image 514
benPearce Avatar asked Sep 29 '10 08:09

benPearce


People also ask

How do I change date picker format?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.


2 Answers

I have solved this problem with a help of this code. Hope it will help you all as well.

<Style TargetType="{x:Type DatePickerTextBox}">  <Setter Property="Control.Template">   <Setter.Value>    <ControlTemplate>     <TextBox x:Name="PART_TextBox"      Text="{Binding Path=SelectedDate, StringFormat='dd MMM yyyy',       RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />    </ControlTemplate>   </Setter.Value>  </Setter> </Style> 
like image 145
petrycol Avatar answered Oct 13 '22 20:10

petrycol


It appears, as per Wonko's answer, that you cannot specify the Date format in Xaml format or by inheriting from the DatePicker.

I have put the following code into my View's constructor which overrides the ShortDateFormat for the current thread:

CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name); ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"; Thread.CurrentThread.CurrentCulture = ci; 
like image 43
benPearce Avatar answered Oct 13 '22 21:10

benPearce