2

I´m doing an app in WPF, C# and EntityFramework 4.0.

I have to validate a date and I would like to show the user if the date is after today something like this:

http://www.nbdtech.com/images/blog/20100621/NiceValidation.png

My code is:

In LoanWindow.xaml:

 <!-- In my window resources -->
      <Style x:Key="datoNoValido" TargetType="{x:Type DatePicker}">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"  Value="Wrong Date"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

    <!-- After some code in the Grid -->
        <DatePicker Style="{StaticResource datoNoValido}" 
                    Name="fecha_SalidaDatePicker" 
                    SelectedDate="{Binding Path=Fecha_Salidad, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}" />

My partial class:

public partial class Loan : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (DateTime.Compare(MyDate, DateTime.Now.Date) < 0)
        {
            ValidationResult vr = new ValidationResult("the message", new[] { "MyDate" });
            this.validationErrors.Add(vr);
        }
        return this.validationErrors;
    }
}

In LoanWindow.xaml.cs:

 //Some code and after
 var errors = p.Validate(null);
 foreach (var item in errors)
 {
   MessageBox.Show(item.ErrorMessage);
 }

The message defined in the partial class (match the name of the entity framework class) shown´s up, but the datapicker never shows the red line. What I´m doin wrong? How should I do it?

Thnks.

1

1 Answer 1

1

On the SelectedDate property the only odd thing i see is that you are not setting the property

    ValidatesOnDataErrors=True

this works for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.