当绑定值为null出现WPF日期选择器验证错误 [英] WPF datepicker validation error appears when binding value is null
问题描述
我有一个WPF应用程序,其中我使用绑定到一个实体框架(与SQL服务器)实体的日期字段日期选择器。我把它绑定如下:
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:
<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.
我的问题是,当底层的数据库字段为空时,我得到的选择日期水印,这是我想要的,但水印被验证,回来为不是日期格式。我想保持水印,但没有验证触发,直到用户改变输入。另外,我想保持 ValidatesOnDataErrors = TRUE
因为我用的是别的地方评估业务逻辑。
My problem is that when the underlying database field is null, I get the select a date watermark, 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=True
because I use that somewhere else to evaluate business logic.
要明白我的意思,这里是一个使用日期选择器与日期null值的一种形式,你可以看到验证错误:
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错误:7:ConvertBack不能转换值'' (类型字符串)。 BindingEx pression:路径= dtComplete; DataItem的='job_BF0D6052EEADCDA3C2B6D1A174D77C322D5AB16A035F214705610767131A80F0 (哈希code = 17930557);目标元素是'DatePicker的 (名称='dtComplete');目标属性是文本(类型字符串) 出现FormatException:System.FormatException:该字符串未被识别为 有效的DateTime。在System.DateTime.Parse(字符串S, 的IFormatProvider提供商)在System.Convert.ToDateTime(字符串 值的IFormatProvider提供商)在 System.Convert.ChangeType(对象的值,类型conversionType, 的IFormatProvider提供商)在 System.Windows.Data.BindingEx pression.ConvertBackHelper(的IValueConverter 转换器,对象的值,类型sourceType的,对象参数, CultureInfo的文化)
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)'
有人可以帮助我摆脱这个验证错误,直到用户改变输入?
Can someone help me get rid of this validation error until the user changes the input?
在此先感谢
推荐答案
想通了:
我被绑定到兴田日期选择器的文本值,因此它试图验证水印文本作为一个日期,但我真的应该一直在做被绑定到selectedDate属性如下:
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:
<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
这篇关于当绑定值为null出现WPF日期选择器验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!