问题描述
我有一个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与日期的空值的表单,您可以看到验证错误:
调试时的输出窗口提供以下验证转换错误:
有人可以帮助我摆脱这个验证错误,直到用户更改输入?
提前感谢
想出来:
绑定到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:
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验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!