0

I have a datePicker, whose Text property I have bound to a view model.

Instead of chosing UpdateSourceTrigger as propertyChanged, I am using Explicit. In the KeyDown event I calling the binding.UpdateSource() method.

The problem i am facing is, when I enter a value 30/10/1983 in the textbox, in the view model, I end up having only the value 30/10/198. I do not get the last entered number, unless I do a lostfocus or tabout.

The moment I enter the first value 3, the view model property gets notified as NULL.

Should I subscribe to some other event?

Is there something wrong which I am doing, can someone help.

Also, I am using string property in the viewmodel instead of DateTime? is this correct approach or not?

Thanks

Sandepku
  • 861
  • 14
  • 31
  • What are you doing in the `KeyDown` event that requires you to update the source explicitly? The issue at hand is that the `KeyDown` event is called before the DatePicker value is updated, but I can't find any events that are raised after. – Sjeijoet Nov 10 '14 at 09:20
  • @hantoun i too am not finding any such event. I need the datepicker to accept user input in the form of dd/mm/yyyy. this entry does not trigger a postback to the view model. but entering the date in the format mm/dd/yyyy does trigger a post back. – Sandepku Nov 10 '14 at 09:52
  • Perhaps [this question](http://stackoverflow.com/questions/3819832/changing-the-string-format-of-the-wpf-datepicker) could help you find the solution you were looking for. I would attempt to set the correct `CultureInfo` instead of messing with static formats, if I were you. In any case, it'll allow you to bind to `DateTime` sources. – Sjeijoet Nov 10 '14 at 10:06
  • @hantoun I tried changing the string format, but its not working for me,it still accepts mm/dd/yyyy and not dd/mm/yyyy – Sandepku Nov 10 '14 at 11:03

1 Answers1

0

Call binding.UpdateSource() inside Dispatcher to make this work. My assumption is datePicker updating the value only after TextChanged event. So moving binding.UpdateSource() inside a Dispatcher call will resolve your problem. http://msdn.microsoft.com/en-us/library/cc190824%28v=vs.110%29.aspx

Sivakumar
  • 478
  • 2
  • 13