0

Simplified Question:

I have a datepicker in WPF. I enter a value 30/10/1983 in the textbox of the datepicker. I need the value to be posted back to my viewmodel, where in I have bound it to a DateTime? property.

Is there any way I can achieve this. mm/dd/yyyy format triggers a postback but not dd/mm/yyyy.

The DatePicker code is as below.

<DatePicker Grid.Row="2" Name="inputDate"
                Text="{Binding BirthDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                >

</DatePicker>

The view model property is as below.

private DateTime? birthDate;

    public DateTime? BirthDate
    {
        get
        {
            return this.birthDate;
        }
        set
        {
            this.birthDate = value;
            OnPropertyChanged("BirthDate");
        }
    }

I enter a value 10/10 into the datepicker textbox, the setter gets called with a value, but the moment I enter an entire date 30/10/1983, I still have the view model property which is equal to NULL.

I have changed the Format to English-UnitedKingdom, in the calendar settings, and my CurrentCulture reflects en-GB appropriately.

Original Question:

I have a datetimepicker control in WPF, which I have bound the text property to a Nullable DateTime in the view model.

The problem I am facing is when I enter a value in the format MM/dd/yyyy, I do get a value postback in the view model, indicating the new value.

But when I enter the value in dd/MM/yyyy format, I do not get a notification in the view model and the value is lost. The bound propertyin the view model is null.

The short date format is the one which I have specified in the calendar settings & dateFormat, within which for a short date format I provide the entry as "dd/MM/yyyy".

Could someone please help me with this scenario where in I accept date in dd/MM/yyyy format, and I do not want to hardcode, I wish to pick up the short date format from the calendar settings and also, I wish to recieve the updated value in the view model too.

Sandepku
  • 861
  • 14
  • 31
  • 1
    post your code or an example – Theodosius Von Richthofen Nov 07 '14 at 15:34
  • @TheodosiusVonRichthofen I have posted a simplified question over here http://stackoverflow.com/questions/26843658/datepicker-wpf-accepting-dd-mm-yyyy-format – Sandepku Nov 10 '14 at 12:21
  • 2
    @Sandepku, you should still show your relevant code, regardless of how simple your question now is. – Sheridan Nov 10 '14 at 16:37
  • Isn't your formatting problem just a `Culture` issue? For example, the default date format that is used in the UK is dd/mm/yyyy, whereas in some places in Europe, it is mm/dd/yyyy. Check the answer to the [How can i globally set the Culture in a WPF Application?](http://stackoverflow.com/questions/1265773/how-can-i-globally-set-the-culture-in-a-wpf-application) question to find out how to change the current `Culture`. – Sheridan Nov 10 '14 at 16:47
  • @Sheridan sorry about that – Sandepku Nov 11 '14 at 03:02
  • @Sheridan I have updated the question with sample code and what I ve tried to do, thank you – Sandepku Nov 11 '14 at 06:45
  • Can you bind the UI Element to a String Property on the view model, and with your code manipulate it to retrieve the correct date and perform validation on the UI? – Stefano Bafaro Nov 11 '14 at 08:40
  • What you are experiencing is the type safe nature of WPF data binding. Your `BirthDate` property will not be updated with an invalid value... if you typed 'hello' into the `DatePicker` control, this would also be prevented from reaching the property. The real question is *why* does WPF think that a valid date is an invalid date on your computer... that's why I thought about the `Culture` on your computer. I cannot reproduce your problem on my computer, as `30/10/1983` is a valid date here. What geographical location is your computer based in? – Sheridan Nov 11 '14 at 09:02
  • @Sheridan you mean to say you type 30/10, the view model gets updated with a value of 30/10/2014, without even losing focus? – Sandepku Nov 11 '14 at 09:27
  • @Sheridan my time zone is UTC+530, Chennai,Kolkota,NewDelhi, I am using English(United Kingdom) format. – Sandepku Nov 11 '14 at 09:28
  • @StefanoBafaro trying that approach, :) thank you! – Sandepku Nov 11 '14 at 10:13

1 Answers1

1

The problem is related that a not valid date will not set with a correct value your ViewModel DateTime property. So, you can intercept it and convert correctly with a CONVERTER.

An example:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = System.Convert.ToString(value);
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;    
    }

And your XAML code will be like:

 <DatePicker 
     Text="{Binding BirthDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
     SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
     Converter={StaticResource DateConverter}}"
  />
Stefano Bafaro
  • 915
  • 10
  • 20
  • DatePicker doesn't have a Text parameter. – Teysz Dec 11 '15 at 15:51
  • @Teysz try look here: https://msdn.microsoft.com/en-us/library/system.windows.controls.datepicker.aspx – Stefano Bafaro Dec 11 '15 at 15:57
  • So I checked and I appareantly use "Windows.UI.Xaml.Controls.DatePicker" and not "System.Windows.Controls.DatePicker". And the one that I use doesn't have the Text parameter. – Teysz Dec 15 '15 at 08:07
  • Perfect, change the Binding to the DATE property instead the TEXT property: https://msdn.microsoft.com/it-it/library/windows/apps/windows.ui.xaml.controls.datepicker.date.aspx – Stefano Bafaro Dec 15 '15 at 14:46
  • I've done what you wrote, yet I do still get the American style date of MM/dd/yyyy. I can't seem to change it to format I want, dd/MM/yyyy. I even changed my windows locale setting from en-US to nl-NL. Still no luck :/ – Teysz Dec 15 '15 at 15:55
  • You can customize the date format! Look here: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.datepicker.dayformat – Stefano Bafaro Dec 16 '15 at 11:55
  • That only formats the day, month and year, I want to format the actual order of them to day month year instead of month day year – Teysz Dec 16 '15 at 12:40
  • Maybe you can solve your issue setting the Culture in your View Constructor like this: CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name); ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"; Thread.CurrentThread.CurrentCulture = ci; – Stefano Bafaro Dec 16 '15 at 13:28
  • using SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text) will cause a "System.StackOverflowException" Exception because it create a circular reference. :( Took me 2 hours to find out what was going on ! – Mr Rubix Jul 25 '17 at 20:10
  • did you find the solution? I've the same issue – KillemAll Feb 25 '21 at 16:14