问题
I use a DateTimePicker on a WPF application. I would like to bind the .Text property to the SelectedDate, so I use binding like this:
<DatePicker x:Name="DateTimePicker_Date"
Text="{Binding dDate, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
TargetNullValue='',
ValidatesOnDataErrors=True}"
TabIndex="3"
Grid.Column="1" />
My problem is I use an European culture, so: DAY/MONTH/YEAR instead of MONTH/DAY/YEAR, so if I input : 14/02/2013
, I have a validation error !
How can I solve this?
回答1:
Solved :
Juste add a :
this.Language = XmlLanguage.GetLanguage("fr-FR");
In the code-behind of the windows :)
回答2:
Very popular topic. I was looking for an answer a lot, I think my decision will suit you. When you create a window, all controls take the default culture from the system. You change the properties SelectedDate = "{Binding MyDate, StringFormat =" ..... "}" SelectedDateFormat = "Short" Maybe you are shown some kind of format that you need but it will not work for input manually edit. To do this, you need to set the culture at the beginning of class initialization
public MyWPFWindow()
{
InitializeComponent();
/// <summary>
/// Change ShortDate on Culture for this <see cref="MyWPFWindow"/>
/// </summary>
CultureInfo cd = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
cd.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
Thread.CurrentThread.CurrentCulture = cd;
}
来源:https://stackoverflow.com/questions/16565652/set-culture-for-a-wpf-datetimepicker-validation