mercredi 6 mai 2015

C# WPF, Datepicker validation

I'm trying to validate date in XAML using validation rules.

<StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal">
    <DatePicker Height="25"  x:Name="DatePickerDate">
        <DatePicker.SelectedDate>
            <Binding Path="ViewModel.Date" NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <validationRules:DatePickerValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </DatePicker.SelectedDate>
    </DatePicker>
</StackPanel>

And Validation rule

public class DatePickerValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        var date = (DateTime) value;

        return date.Date.CompareTo(DateTime.Now) < 0
            ? new ValidationResult(false, "the date can not be before today")
            : new ValidationResult(true, null);
    }
}

But when I put breakpoint into validation rule, it never goes there even if I change the Date.

Since I am new to WPF, it would be appreciable if any suggestions or guidance are available here.

Thanks.

Aucun commentaire:

Enregistrer un commentaire