Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968590
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:51:41+00:00 2026-05-20T03:51:41+00:00

I have read a lot of Blog post on WPF Validation and on DataAnnotations

  • 0

I have read a lot of Blog post on WPF Validation and on DataAnnotations. I was wondering if there is a clean way to use DataAnnotations as ValidationRules for my entity.

So instead of having this (Source) :

<Binding Path="Age" Source="{StaticResource ods}" ... >
  <Binding.ValidationRules>
    <c:AgeRangeRule Min="21" Max="130"/>
  </Binding.ValidationRules>
</Binding>

Where you must have your

public class AgeRangeRule : ValidationRule 
{...}

I want the WPF Binding to go see the Age property and look for DataAnnotation a bit like this:

[Range(1, 120)]
public int Age
{
  get { return _age; }
  set
  {
    _age = value;
    RaisePropertyChanged<...>(x => x.Age);
  }
}

Any ideas if this is possible ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:51:42+00:00Added an answer on May 20, 2026 at 3:51 am

    The closest approach I found is :

    // This loop into all DataAnnotations and return all errors strings
    protected string ValidateProperty(object value, string propertyName)
    {
      var info = this.GetType().GetProperty(propertyName);
      IEnumerable<string> errorInfos =
            (from va in info.GetCustomAttributes(true).OfType<ValidationAttribute>()
             where !va.IsValid(value)
             select va.FormatErrorMessage(string.Empty)).ToList();
    
    
      if (errorInfos.Count() > 0)
      {
        return errorInfos.FirstOrDefault<string>();
      }
      return null;
    

    Source

    public class PersonEntity : IDataErrorInfo
    {
    
        [StringLength(50, MinimumLength = 1, ErrorMessage = "Error Msg.")]
        public string Name
        {
          get { return _name; }
          set
          {
            _name = value;
            PropertyChanged("Name");
          }
        }
    
    public string this[string propertyName]
        {
          get
          {
            if (porpertyName == "Name")
            return ValidateProperty(this.Name, propertyName);
          }
        }
    }
    

    Source and Source

    That way, the DataAnnotation works fine, I got a minimum to do on the XAML ValidatesOnDataErrors="True" and it’s a fine workaround of Aaron post with the DataAnnotation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a lot that LISP can redefine syntax on the fly, presumably
I have read a lot of popular standards manuals for open source PHP projects.
I have read a lot of articles on Dependency Injection as well as watched
I have read this post about how to test private methods. I usually do
I just read a blog post that explains MVC with a banking analogy. I
I have read a lot about mocking, particularly using Rhino Mocks and have learned
I have read a lot of the MongoDB. I like all the features it
I have already read a lot about C++ exceptions and what i see, that
I have read through several reviews on Amazon and some books seem outdated. I
I have read about partial methods in the latest C# language specification , so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.