我可能会很累,但是为什么会抛出异常:

 this.SomeDatePicker.SelectedDate = DateTime.Now.Date;

同样发生在:
 this.SomeDatePicker.SelectedDate = DateTime.Now;

错误消息是:
System.ArgumentOutOfRangeException was unhandled
  Message=SelectedDate value is not valid.
Parameter name: d
  Source=PresentationFramework
  ParamName=d
  StackTrace:
       at System.Windows.Controls.Calendar.OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.Controls.DatePicker.CoerceSelectedDate(DependencyObject d, Object value)
       at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean skipBaseValueChecks)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       ....

最佳答案

如果您设置的日期在 BlackoutDates 中,则抛出该异常。
AddDaysInPast方法应等效于:

Add(new CalendarDateRange(DateTime.MinValue, DateTime.Today.AddDays(-1)));

因此,除非当前时间在AddDaysInPast调用和SelectedDate分配之间改变,否则您应该不会遇到您描述的问题。

在调试中,获取BlackoutDates中存在的完整范围并使用该信息更新您的问题,您可以使用类似以下内容的方法:
string ranges = string.Join(
    Environment.NewLine,
    DatePicker.BlackoutDates.Select(r => string.Concat(r.Start, "|", r.End)));

关于c# - Message = SelectedDate值无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12114968/

10-11 15:58