Message = SelectedDate is invalid

I may be tired, but why this throws an exception:

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

The same thing happens with:

 this.SomeDatePicker.SelectedDate = DateTime.Now;

Error message:

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)
       ....
+5
source share
4 answers

This exception is thrown if the date you set is present at BlackoutDates.

The method AddDaysInPastshould be equivalent to:

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

therefore, if the current time does not change between the call AddDaysInPastand SelectedDate, you should not have the problem that you described.

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

string ranges = string.Join(
    Environment.NewLine,
    DatePicker.BlackoutDates.Select(r => string.Concat(r.Start, "|", r.End)));
+5
source

DateTime.Now , DisplayDateStart DisplayDateEnd , BlackoutDates, .

+1

:

ArgumentOutOfRangeException: , DisplayDateStart DisplayDateEnd, BlackoutDates.

: http://msdn.microsoft.com/pt-br/library/system.windows.controls.datepicker.selecteddate.aspx

!

?

DateTime.Now.ToString("yyyy-MM-dd"); // only date

DateTime.Now.ToString("yyyy-MM-dd hh:mm"); // date and hour
0

, João Angelo, Ryan Ascension, :

if ( IsInmediate ) 
{
     SomeDatePicker.BlackoutDate.Clear();
     SomeDatePicker.SelectedDate = DateTime.Now;
     SomeDatePicker.BlackoutDate.AddDatesInPast();
}  
0

All Articles