我可能会感到厌倦,但为什么这会引发例外:
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)
....
答案 0 :(得分:5)
如果您设置的日期出现在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)));
答案 1 :(得分:1)
如果DateTime.Now
超出了由BlackoutDates
中包含的DisplayDateStart和DisplayDateEnd OR定义的范围,则抛出异常。
答案 2 :(得分:0)
查找
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
答案 3 :(得分:0)
谢谢JoãoAngelo,Ryan的回答和Ascension答案我能得到理想的结果:
if ( IsInmediate )
{
SomeDatePicker.BlackoutDate.Clear();
SomeDatePicker.SelectedDate = DateTime.Now;
SomeDatePicker.BlackoutDate.AddDatesInPast();
}