简体   繁体   English

Message = SelectedDate值无效

[英]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. 因此,除非当前时间在调用AddDaysInPastSelectedDate赋值之间发生变化,否则您不应该遇到所描述的问题。

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.

相关问题 本地化消息“值'aaa'对于……无效” - Localization message “The value 'aaa' is not valid for …” 为什么DatePicker SelectedDate的默认值会导致StackOverflow异常? - Why does default value for DatePicker SelectedDate cause StackOverflow exception? C#获取SelectedDate.Day字符串值并将其绑定到gridview - C# Get the SelectedDate.Day String value and bind it to gridview 如何在XAML中实现SelectedDate.Value.AddDay()方法? - How to implement the SelectedDate.Value.AddDay() methode in XAML? 日历延长器 SelectedDate Null - Calendar Extender SelectedDate Null 使用SelectedDate添加到BlackoutDate - Adding to BlackoutDates using SelectedDate ExceptionType:“System.FormatException” 消息:“30/6/2019 不是 DateTime 的有效值。” - ExceptionType: "System.FormatException" Message: "30/6/2019 is not a valid value for DateTime." ONVIF反序列化操作'GetStatus'的回复消息正文时出错->字符串'1532531507'不是有效的AllXsd值 - ONVIF Error in deserializing body of reply message for operation 'GetStatus' -> The string '1532531507' is not a valid AllXsd value “未找到“SelectedDate”的属性、BindableProperty 或事件,并且“未在“DatePicker”类型中找到“SelectedDate”属性” - 'No property, BindableProperty, or event found for "SelectedDate"' and 'The Property "SelectedDate" Was not found in type "DatePicker"' 消息错误“指定的转换无效” - message error “Specified cast is not valid”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM