日期选择器验证 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2610883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:40:26  来源:igfitidea点击:

Date picker validation WPF

wpfvalidationdatepicker

提问by developer

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.

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

采纳答案by jonny

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

我不是 WPF 专家,但我会试着给你一个想法

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.

发生验证错误时指定控制模板。默认情况下,验证错误会更改边框颜色。当鼠标超出控制时,我使用了额外的工具提示。

alt text

替代文字

source code

源代码

About 'picker to picker' validation.

关于“picker to picker”验证。

I know that one can use custom properties in validation rules (see AgeRangeRule in msdnexample)

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

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

但是为了应用绑定,您需要使 MaxDate 成为 DependencyProperty .. 您应该明确地询问大师 ;)

Instead of highlighting you should consider intercepting the datepicker value change (via some kind of datepicker 'onchange' event) and accept or reject the change.

您应该考虑拦截 datepicker 值更改(通过某种 datepicker 'onchange' 事件)并接受或拒绝更改,而不是突出显示。