开发者

WPF toolkit, change date format in DatePicker

default date format in DatePicker is "yyyy-mm-dd开发者_运维技巧" but i need "dd-mm-yyyy". This one solution doesn't work for me.


This is a pretty old question (marked somewhat incorrectly as "answered"). If you need to use a specific date format for the WPF DatePicker control but don't want to open up the source-code for it (which is, in fact, available for download), you can always specify the DateFormat of your entire UI, or set it for each DatePicker control.

In my app, I used the following in Application_Startup event:

Private Sub Application_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles MyBase.Startup

   ' other application initialization code here

   Dim culInfo As Globalization.CultureInfo = New System.Globalization.CultureInfo("en-us")
   Dim dtinfo As Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo() With {
       .ShortDatePattern = "dd-MMM-yyyy",
       .ShortTimePattern = "hh:mm:ss tt",
       .TimeSeparator = ":",
       .MonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.MonthNames(),
       .AbbreviatedMonthNames = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.AbbreviatedMonthNames(),
       .DateSeparator = "-"}

 culInfo.DateTimeFormat = dtinfo
 System.Threading.Thread.CurrentThread.CurrentCulture = culInfo

Note that this will set the format for short-date-format everywhere in your app (which is what I wanted, rather than going through and applying the culture to each and every instance of a date-picker. I came up with the above based on another StackOverflow post; I don't remember exactly where it was (or I'd credit the author), but anyway, hope this helps!

Kirk


If you want to give that format to your date your must do something like this:

DatePicker datetimepicker;
datetimepicker.SelectedDate.Value.ToString("yyyy-MM-dd");

datetimepicker.SelectedDate.Value is a DateTime Object so you can manipulate as you want.


The date is displayed using the CultureInfo.CurrentCulture, that is the Windows regional settings the user selected in the control panel and it seems that can't be changed. However it shouldn't really matter since it's the user preferred date/time setting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜