问题描述
如何将验证应用于WPF datepicker工具包?如果选择无效日期,我想将其错误出现,在某些情况下,我有抵达和离开日期,所以我想验证它是否到达日期不晚于出发日期。
似乎是一年以上的日期选择器验证示例)
也许你应该使用这样的功能
< local:MaxDateRule MaxDate ={Binding ElementName = DepartureDatePicker,Path = SelectedDate/>
但是为了应用绑定,您需要使MaxDate成为DependencyProperty ..你应该明确地问一个大师;)
而不是突出显示,您应该考虑截取datepicker值更改(通过某种datepicker'onchange'事件),并接受或拒绝更改。
How to apply validations to WPF datepicker toolkit? I want it to error out if invalid date is selected and in some case I have Arrival and Departure dates, so I want to validate it to see that the arrival date is not later than the departure date.
It seems a year above date picker validation was a problem. Anyway, now it works.
I am not a WPF specialist, bu I'll try to give you an idea
write a validation rule
public class DateExpiredRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
DateTime orderDate = (DateTime)value;
return new ValidationResult(orderDate < DateTime.Now, "Please, enter date before Now()");
}
}
then you can attach it to datepicker
<!-- since validation works hand by hand with binding,
I use hidden datepicker as binding source -->
<WPFToolkit:DatePicker Name="dateProvider" Visibility="Collapsed">
</WPFToolkit:DatePicker>
<WPFToolkit:DatePicker Name="notExpired">
<WPFToolkit:DatePicker.SelectedDate>
<Binding ElementName="dateProvider" Path="SelectedDate" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:DateExpiredRule/>
</Binding.ValidationRules>
</Binding>
</WPFToolkit:DatePicker.SelectedDate>
</WPFToolkit:DatePicker>
specify control template when validation error occurs. By default validation error changes border color. I used additional tooltip when mouse is over control.
About 'picker to picker' validation.
I know that one can use custom properties in validation rules (see AgeRangeRule in msdn example)
Maybe you should use this feature like this
<local:MaxDateRule MaxDate="{Binding ElementName=DepartureDatePicker, Path=SelectedDate" />
but in order to apply binding you need to make MaxDate a DependencyProperty .. you should definetly ask a guru ;)
Instead of highlighting you should consider intercepting the datepicker value change (via some kind of datepicker 'onchange' event) and accept or reject the change.
这篇关于日期选择器验证WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!