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.
On the SelectedDate property the only odd thing i see is that you are not setting the property
ValidatesOnDataErrors=True
this works for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.