启动时Wpf DatePicker验证错误 [英] Wpf DatePicker validation error on startup

查看:268
本文介绍了启动时Wpf DatePicker验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的wpf datepicker框,其中绑定了 SelectedDate

 < DatePicker x:Name =EffDatePickerSelectedDate ={Binding EffectiveDate,Mode = OneWayToSource}/> 

当页面加载时,日期戳显示为红色边框,表明验证失败,可能是因为EffectiveDate没有设置。





我可以在启动时设置生效日期,但是丢失内置的选择日期水印,我想保留。我也可以在绑定中设置TargetNullValue,但是我再次丢失该水印。我使用OneWayToSource,因为日期只能由用户设置,永远不会通过模型。也许我使用不正确?



当用户选择日期时,它会正确设置模型中的 EffectiveDate 属性,错误消失



我知道我可以将验证错误模板设置为 {x:Null} / em>那个红色的边框被显示出来,但感觉很黑。我也看过这个问题,但是看起来这个问题来自绑定到文本而不是 SelectedDate



任何人都可以建议正确的方法修复验证错误,而不仅仅是隐藏?



提前感谢

解决方案

您可以编写自己的 ValidationRule DatePicker 一起使用。 p>

假设您的viewmodel中有可空的 DateTime 属性,例如



公共类MainViewModel:ViewModelBase
{
public MainViewModel()
{}

public DateTime? EffectiveDate {get;组; }
}

而在您的XAML中,您有

  ... 
xmlns:rules =clr-namespace:YourNameSpace.ValidationRules
...
< DatePicker x :Name =EffDatePicker>
< DatePicker.SelectedDate>
< Binding Path =EffectiveDate
Mode =OneWayToSource
UpdateSourceTrigger =PropertyChanged>
< Binding.ValidationRules>
< rules:DateRule />
< /Binding.ValidationRules>
< / Binding>
< /DatePicker.SelectedDate>
< / DatePicker>

然后,以下规则将伎俩

 命名空间YourNameSpace.ValidationRules 
{
public class DateRule:ValidationRule
{
public override ValidationResult验证(对象值,CultureInfo cultureInfo)
{
返回新的ValidationResult(值为DateTime || value == null,null);
}
}
}


I have a standard wpf datepicker box where the SelectedDate is bound:

<DatePicker x:Name="EffDatePicker" SelectedDate="{Binding EffectiveDate, Mode=OneWayToSource}"/>

When the page loads, the datepicker is getting highlighted with a red border, suggesting that the validation has failed, probably because the EffectiveDate is not set.

I can set the effective date on startup, but then I lose the built in "Select a date" watermark, which I'd like to keep. I can also set the TargetNullValue in the binding, but then I lose that watermark again. I'm using OneWayToSource because the date is only ever set by the user, never through the model. Perhaps I'm using that incorrectly?

When the user selects a date, it does properly set the EffectiveDate property in the model and the error goes away.

I know I can set the validation error template to {x:Null} and suppress that red border from being shown, but that feels hacky. I have also seen this question, but it looks like that problem came from binding to Text instead of SelectedDate.

Can anyone suggest the right way to fix the validation error and not just hide it?

Thanks in advance,

解决方案

You could write your own ValidationRule to use with DatePicker.

Let's say you have nullable DateTime property in your viewmodel, e.g.

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {}

    public DateTime? EffectiveDate { get; set; }
}

And in your XAML you have

...
xmlns:rules="clr-namespace:YourNameSpace.ValidationRules"
...
<DatePicker x:Name="EffDatePicker">
    <DatePicker.SelectedDate>
        <Binding Path="EffectiveDate" 
                 Mode="OneWayToSource" 
                 UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <rules:DateRule/>
            </Binding.ValidationRules>
        </Binding>
    </DatePicker.SelectedDate>
</DatePicker>

Then the following rule would the trick

namespace YourNameSpace.ValidationRules
{
    public class DateRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            return new ValidationResult(value is DateTime || value == null, null);
        }
    }
}

这篇关于启动时Wpf DatePicker验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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