当绑定值为null时,会出现WPF datepicker验证错误 [英] WPF datepicker validation error appears when binding value is null

查看:279
本文介绍了当绑定值为null时,会出现WPF datepicker验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,我使用绑定到实体框架(使用SQL Server)实体的日期字段的日期选择器。我绑定如下:

 < DatePicker x:Name =dtCompleteStyle ={StaticResource FTC_DatePicker}Grid。 Column =2Grid.Row =7Grid.ColumnSpan =3
Text ={Binding dtComplete,Mode = TwoWay,ValidatesOnDataErrors = True}/>

绑定工作正常,可以更新到实体。



我的问题是,当底层数据库字段为空时,我得到选择一个日期水印,这是我想要的,但是该水印正在被验证并返回,因为不是日期格式。我想保留水印,但是在用户更改输入之前不具有验证触发器。此外,我想保留 ValidatesOnDataErrors = True ,因为我在其他地方使用它来评估业务逻辑。



要查看我的意思是,这是一个使用datepicker与日期的空值的表单,您可以看到验证错误:



调试时的输出窗口提供以下验证转换错误:


System.Windows.Data错误:7:ConvertBack不能转换值''
(输入'String')。 BindingExpression:路径= dtComplete;
DataItem ='job_BF0D6052EEADCDA3C2B6D1A174D77C322D5AB16A035F214705610767131A80F0'
(HashCode = 17930557); target元素是'DatePicker'
(Name ='dtComplete'); target属性为'Text'(类型'String')
FormatException:'System.FormatException:String未被识别为
一个有效的DateTime。在System.Convert.ToDateTime(String
值,IFormatProvider提供程序)在
System.Convert.ChangeType(Object value,Type conversionType)中的System.DateTime.Parse(String s,
IFormatProvider provider) ,
IFormatProvider提供程序)
System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter
转换器,对象值,类型sourceType,对象参数,
CultureInfo文化)'


有人可以帮助我摆脱这个验证错误,直到用户更改输入?



提前感谢

解决方案

想出来:



绑定到hte datePicker的文本值,所以它试图验证水印文本作为一个日期,但我真正应该做的是绑定到selectedDate属性如下:

 < DatePicker x:Name =dtCompleteStyle ={StaticResource FTC_DatePicker}Grid.Column =2Grid.Row =7Grid.ColumnSpan =3
SelectedDate ={Binding dtComplete,Mode = TwoWay,ValidatesOnDataErrors = True}/>

现在按照我的要求行事,选择日期水印仍然存在,绑定日期值正常工作。现在太明显了简单修复


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.

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.

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)'

Can someone help me get rid of this validation error until the user changes the input?

Thanks in advance

解决方案

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:

<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 datepicker验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆