Set culture for a WPF DateTimePicker validation

不想你离开。 提交于 2020-01-14 10:36:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!