绑定值为空时出现 WPF 日期选择器验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15298781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF datepicker validation error appears when binding value is null
提问by J King
I have a WPF application where I am using a date picker that is bound to an entity framework(with SQL server) entity's date field. I bind it as follows:
我有一个 WPF 应用程序,我在其中使用绑定到实体框架(带有 SQL 服务器)实体的日期字段的日期选择器。我将其绑定如下:
<DatePicker x:Name="dtComplete" Style="{StaticResource FTC_DatePicker}" Grid.Column="2" Grid.Row="7" Grid.ColumnSpan="3"
Text="{Binding dtComplete, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
The binding works fine and can be updated to the entity.
绑定工作正常,可以更新到实体。
My problem is that when the underlying database field is null, I get the select a datewatermark, which I want, but that watermark is being validated and coming back as not being of a date format. I want to keep the watermark, but not have the validation trigger until the user changes the input. Also, I want to keep ValidatesOnDataErrors=Truebecause I use that somewhere else to evaluate business logic.
我的问题是,当基础数据库字段为空时,我会选择一个日期水印,这是我想要的,但该水印正在验证并返回为不是日期格式。我想保留水印,但在用户更改输入之前没有验证触发器。另外,我想保留,ValidatesOnDataErrors=True因为我在其他地方使用它来评估业务逻辑。
To see what I mean, here is a form that uses the datepicker with a null value for the date, you can see the validation error:

要明白我的意思,这里是一个使用日期选择器的表单,日期为空值,您可以看到验证错误:

The output window when debugging gives the following validation conversion error:
调试时的输出窗口给出了以下验证转换错误:
System.Windows.Data Error: 7 : ConvertBack cannot convert value '' (type 'String'). BindingExpression:Path=dtComplete; DataItem='job_BF0D6052EEADCDA3C2B6D1A174D77C322D5AB16A035F214705610767131A80F0' (HashCode=17930557); target element is 'DatePicker' (Name='dtComplete'); target property is 'Text' (type 'String') FormatException:'System.FormatException: String was not recognized as a valid DateTime. at System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
System.Windows.Data 错误:7:ConvertBack 无法转换值 ''(类型 'String')。BindingExpression:Path=dtComplete; DataItem='job_BF0D6052EEADCDA3C2B6D1A174D77C322D5AB16A035F214705610767131A80F0'(哈希码=17930557);目标元素是 'DatePicker' (Name='dtComplete'); 目标属性是 'Text'(类型 'String') FormatException:'System.FormatException: String 未被识别为有效的 DateTime。在 System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Windows.Data.BindingExpression .ConvertBackHelper(IValueConverter 转换器,对象值,类型源类型,对象参数,
Can someone help me get rid of this validation error until the user changes the input?
在用户更改输入之前,有人可以帮助我摆脱此验证错误吗?
Thanks in advance
提前致谢
回答by J King
Figured it out:
弄清楚了:
I was binding to the text value of hte datePicker, so it was trying to validate the watermark text as a date, but what I really should have been doing was binding to the selectedDate Property as follows:
我绑定到 hte datePicker 的文本值,因此它试图将水印文本验证为日期,但我真正应该做的是绑定到 selectedDate 属性,如下所示:
<DatePicker x:Name="dtComplete" Style="{StaticResource FTC_DatePicker}" Grid.Column="2" Grid.Row="7" Grid.ColumnSpan="3"
SelectedDate="{Binding dtComplete, Mode=TwoWay, ValidatesOnDataErrors=True}" />
This now behaves as I want, the select a date watermark is still there and the binding date value works. Its all too obvious now. Simple fix
这现在表现得如我所愿,选择日期水印仍然存在并且绑定日期值有效。现在一切都太明显了。简单的修复
回答by Emily
The problem is actually that the standard behavior of the datepicker is to use the selected value to highlight the day on the calendar. Since you are putting a non-date value in the value property, when the calendar is activated, it doesn't know what to do. There is an event "dropdown" that fires when the datepicker calendar is opened. You could set the value to today once it is opened, which will remove your error.
What I'm not clear on is how you are getting that text to display in the first place. I thought the datepicker validated any value entered into it.
问题实际上是日期选择器的标准行为是使用选定的值来突出显示日历上的日期。由于您在 value 属性中放置了一个非日期值,当日历被激活时,它不知道该怎么做。打开日期选择器日历时会触发一个事件“下拉列表”。您可以在打开后将该值设置为今天,这将消除您的错误。
我不清楚的是,您首先是如何让该文本显示出来的。我认为日期选择器验证了输入的任何值。

