日期选择器验证 WPF [英] Date picker validation WPF

查看:19
本文介绍了日期选择器验证 WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将验证应用于 WPF 日期选择器工具包?我希望它在选择无效日期时出错,并且在某些情况下我有到达和出发日期,所以我想验证它以查看到达日期不晚于出发日期.

解决方案

似乎比日期选择器验证高一年

关于picker to picker"验证.

我知道可以在验证规则中使用自定义属性(请参阅 msdn 示例)

也许你应该像这样使用这个功能

但是为了应用绑定,您需要使 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.

source code

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

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