且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

启动时Wpf DatePicker验证错误

更新时间:2023-02-26 13:44:43

您可以编写自己的 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);
        }
    }
}