is it possible to use System.ComponentModel.DataAnnotations
and it's belong attribute(such as Required
,Range
,...) in WPF or Winforms class?
I want put my validation to attributs.
thanks
EDIT 1:
I write this :
public class Recipe
{
[Required]
[CustomValidation(typeof(AWValidation), "ValidateId", ErrorMessage = "nima")]
public int Name { get; set; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var recipe = new Recipe();
recipe.Name = 3;
var context = new ValidationContext(recipe, serviceProvider: null, items: null);
var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
var isValid = Validator.TryValidateObject(recipe, context, results);
if (!isValid)
{
foreach (var validationResult in results)
{
MessageBox.Show(validationResult.ErrorMessage);
}
}
}
public class AWValidation
{
public bool ValidateId(int ProductID)
{
bool isValid;
if (ProductID > 2)
{
isValid = false;
}
else
{
isValid = true;
}
return isValid;
}
}
but even I set 3 to my property nothing happend