8

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

Arian
  • 12,793
  • 66
  • 176
  • 300
  • Duplicate Question at : http://stackoverflow.com/questions/1755340/validate-data-using-dataannotations-with-wpf-entity-framework – Raghu Jul 28 '11 at 06:48

1 Answers1

10

Yes, you can. And here's another article illustrating this. You could do this even in a console application by manually creating a ValidationContext:

public class DataAnnotationsValidator
{
    public bool TryValidate(object @object, out ICollection<ValidationResult> results)
    {
        var context = new ValidationContext(@object, serviceProvider: null, items: null);
        results = new List<ValidationResult>();
        return Validator.TryValidateObject(
            @object, context, results, 
            validateAllProperties: true
        );
    }
}

UPDATE:

Here's an example:

public class Recipe
{
    [Required]
    [CustomValidation(typeof(AWValidation), "ValidateId", ErrorMessage = "nima")]
    public int Name { get; set; }
}

public class AWValidation
{
    public static ValidationResult ValidateId(int ProductID)
    {
        if (ProductID > 2)
        {
            return new ValidationResult("wrong");
        }
        else
        {
            return ValidationResult.Success;
        }
    }
}

class Program
{
    static void Main()
    {
        var recipe = new Recipe();
        recipe.Name = 3;
        var context = new ValidationContext(recipe, serviceProvider: null, items: null);
        var results = new List<ValidationResult>();

        var isValid = Validator.TryValidateObject(recipe, context, results, true);

        if (!isValid)
        {
            foreach (var validationResult in results)
            {
                Console.WriteLine(validationResult.ErrorMessage);
            }
        }
    }
}

Notice that the ValidateId method must be public static and return ValidationResult instead of boolean. Also notice a fourth argument passed to the TryValidateObject method which must be set to true if you want your custom validators to be evaluated.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Nice, +1 for the article reference. – WPF-it Jul 28 '11 at 06:22
  • Thanks but I don't undestand your code correctly.how your code work?and for what?I want when for Example Filed1 has value 2 en error message returns – Arian Jul 28 '11 at 06:49
  • @Nima, I have updated my answer to provide you with an example. – Darin Dimitrov Jul 28 '11 at 07:15
  • Thank you very much.that's great.but I don't undersant a bit of code.If I set `ErrorMessage` in Attribute why when validation not success the "Wrong" word displays instead of "nima"? – Arian Jul 28 '11 at 07:22