date difference using datepicker in wpf

后端 未结 2 1099
死守一世寂寞
死守一世寂寞 2020-12-21 09:29

Im trying to calculate the how many days there are between two dates and display the result in a textblock, i am using wpf. However i get a nullable object must have a value

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 10:07

    If you want to get the selected date, you're using the wrong property. DisplayDateStart and DisplayDateEnd limits the possible choices. They are nullable because it should be possible to create a date picker without any limitations. Change your start and finish assignments to use the DisplayDate property instead:

    DateTime start = datePicker1.DisplayDate.Date;
    DateTime finish = datePicker2.DisplayDate.Date;
    

    You should also consider renaming your datepickers. Having controls named datePicker1 and datePicker2 is typically hard to remember. What about datePickerStart and datePickerEnd?

    (Disclaimer: I know that WPF controls typically don't get a name, because they are databound so the name won't matter, but in this case they are explicitly accessed and should have proper names).

提交回复
热议问题