是否有可能使用System.ComponentModel.DataAnnotations
及其屬於屬性(如Required
,Range
,...)在WPF或Winforms類?如何使用WPF或Winforms應用程序中的System.ComponentModel.DataAnnotations
我想把我的驗證屬性。
感謝
編輯1:
我寫這篇文章:
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;
}
}
但即使我設置3到我的財產什麼happend
重複的問題在:http://stackoverflow.com/questions/1755340/validate-data-using-dataannotations-with-wpf-entity-framework – Raghu