I’ve an oddity with the WPF DatePicker that I’ve demonstrated in a project downloadable from here.
I need a datepicker styled to look like normal text, with no button or outline, to pop up the calendar when a seperate button’s pressed, to let the user edit the text, and to show the date in the format we want- dd MMM yyyy.
What I have almost works, but when losing focus the date format reverts either dd/mm/yyyy or mm/dd/yyyy, alternating each time focus is lost (i.e. clicking on the date and tabbing out without editing), until the control is next updated completely independently from code, which pressing “Rewrite” does.
Can anyone shed some light on what’s going on and how to fix it?
The
DatePickercontrol handles theLostFocusevent of theDatePickerTextBox, parses the text as aDateTime, formats the parsedDateTimeusing either the long or short date format from the current culture, and then updates the text. This will override the binding on the text property, so your custom format won’t be used.Short of writing your own
DatePickercontrol, it seems the only solution is to change the short date pattern on theCurrentCulture: https://stackoverflow.com/a/3869415/124386