[英]Message=SelectedDate value is not valid
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)
....
That exception is thrown if the date you're setting is present in BlackoutDates
. 如果
BlackoutDates
存在您设置的日期,则抛出该异常。
The AddDaysInPast
method should be equivalent to: AddDaysInPast
方法应该等效于:
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. 因此,除非当前时间在调用
AddDaysInPast
和SelectedDate
赋值之间发生变化,否则您不应该遇到所描述的问题。
In debug get the complete ranges present in BlackoutDates
and update your question with that information, you can use something like: 在调试中获取
BlackoutDates
的完整范围并使用该信息更新您的问题,您可以使用以下内容:
string ranges = string.Join(
Environment.NewLine,
DatePicker.BlackoutDates.Select(r => string.Concat(r.Start, "|", r.End)));
如果DateTime.Now
超出BlackoutDates
包含的DisplayDateStart和DisplayDateEnd OR定义的范围,则抛出异常。
Look: 看:
ArgumentOutOfRangeException: The specified date is not in the range defined by DisplayDateStart and DisplayDateEnd, or the specified date is in the BlackoutDates collection. ArgumentOutOfRangeException:指定的日期不在DisplayDateStart和DisplayDateEnd定义的范围内,或者指定的日期在BlackoutDates集合中。
Font: http://msdn.microsoft.com/pt-br/library/system.windows.controls.datepicker.selecteddate.aspx 字体: http : //msdn.microsoft.com/pt-br/library/system.windows.controls.datepicker.selecteddate.aspx
Look Exceptions! 看看例外!
You need of date and time or date only? 您只需要日期和时间或日期吗?
DateTime.Now.ToString("yyyy-MM-dd"); // only date
or 要么
DateTime.Now.ToString("yyyy-MM-dd hh:mm"); // date and hour
Thanks you João Angelo, Ryan answers and Ascension answers I could get the desired result: 谢谢JoãoAngelo,Ryan回答和Ascension答案,我可以得到理想的结果:
if ( IsInmediate )
{
SomeDatePicker.BlackoutDate.Clear();
SomeDatePicker.SelectedDate = DateTime.Now;
SomeDatePicker.BlackoutDate.AddDatesInPast();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.