Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message=SelectedDate value is not valid

Tags:

c#

wpf

datepicker

I might be tired but why it this throwing an exception:

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

Same happens with:

 this.SomeDatePicker.SelectedDate = DateTime.Now;

The error message is:

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)
       ....
like image 264
OscarRyz Avatar asked Aug 24 '12 18:08

OscarRyz


1 Answers

That exception is thrown if the date you're setting is present in BlackoutDates.

The AddDaysInPast method should be equivalent to:

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

so unless the current time changes between the call to AddDaysInPast and the SelectedDate assignment you should not be having the problem you described.

In debug get the complete ranges present in BlackoutDates and update your question with that information, you can use something like:

string ranges = string.Join(
    Environment.NewLine,
    DatePicker.BlackoutDates.Select(r => string.Concat(r.Start, "|", r.End)));
like image 178
João Angelo Avatar answered Nov 15 '22 00:11

João Angelo