Set culture for a WPF DateTimePicker validation
Asked Answered
T

2

6

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?

Trumaine answered 15/5, 2013 at 12:58 Comment(0)
T
8

Solved :

Juste add a :

this.Language = XmlLanguage.GetLanguage("fr-FR");

In the code-behind of the windows :)

Trumaine answered 15/5, 2013 at 13:10 Comment(1)
Thank you so much!Panettone
R
0

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;
    }
Riesman answered 14/12, 2018 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.