I have the following definition for a DatePicker:
<sdk:DatePicker x:Name="dtpStartDate" Grid.Row="4" Grid.Column="1" SelectedDateFormat="Short" SelectedDate="{Binding MyObject.StartDate, Mode=TwoWay, NotifyOnValidationError=True}"/> MyObject is a class that contains the StartDate which is defined as a nullable DateTime.
class MyObjectClass { .... public DateTime? StartDate { get; set; } .... } I'm using MVVM (via Prism) and MyObject is correctly bound.
The first time I display this view the date is blank (as expected) and when I click on the picker the calendar is displayed with today's date highlighted.
If I select a date (other than today) or display a record with a date filled then when I blank the form to create a new object:
this.MyObject = new MyObjectClass(); the date hightlighted when I click on the picker is the previously selected date.
So if the record showed "1st May 2009" that's what would be highlighted in the picker. I can set StartDate to DateTime.Today which is correctly displayed, but does mean that a date is displayed in the form which I don't want.
So, why doesn't setting the date to null reset the selected date in the picker to today?
Answer: 1
Not sure if you just left this out from your question, but if you want programmatic changes to show up on the UI, your ViewModel and MyObjectClass should implement INotifyPropertyChanged. Specifically, the VM's MyObject property and MyObjectClass StartDate property setters should fire the PropertyChanged event.
by : fosonhttp://stackoverflow.com/users/22539Answer: 2
The solution is to reset the DisplayDate property on the DatePicker:
this.SelectedDateChanged += (s, e) => { if (this.SelectedDate == null) { this.DisplayDate = DateTime.Today; this.Text = string.Empty; } }; To add this functionality we had to subclass the DataPicker though.
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog