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

  • SEARCH
  • Home
  • 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 8355609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:55:04+00:00 2026-06-09T09:55:04+00:00

In the WPF project I am working on I am considering implementing data validation

  • 0

In the WPF project I am working on I am considering implementing data validation through attributes on properties of my view models.

Example:

[RegexConstraint("^[A-Z][a-zA-Z]+$")]
public string Name 
{
    set
    {
         _name = value;
         OnPropertyChanged("Name")
    }
}

All my view models inherit from a common ViewModelBase class, which implements IDataErrorInfo. The indexer:

string IDataErrorInfo.this[string columnName]

retrieves properties by name (using reflection),

var properties = GetType().GetProperties().Where(p=> p.Name == "someName")

finds all constraint attributes…

private static ICollection<IValidator> GetValidations(PropertyInfo property)
{
 return (ValidationAttribute[])property.GetCustomAttributes(typeof(ValidationAttribute), true))
            .Select(va => new AttributeValidator(va)).Cast<IValidator>().ToList();
}

… and performs validation

It seems to work, but my question is – does anyone have experience using a similar technique? Is that a bad idea?

It does make my code seem cleaner and more concise, and I avoid having to implement IDataErrorInfo in all my view model classes, but on the other hand it creates a new problem – how to construct pretty user messages, when the validation logic relies on property names / attribute names rather than using hard-coded messages – like all the IDataErrorInfo examples I was able to find.

Summing up – my question is – should I continue going down this road, or should I use a different validation scheme?

  • 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-06-09T09:55:06+00:00Added an answer on June 9, 2026 at 9:55 am

    I have done this similar technique on a couple of different projects and you can control the error message on most of the DataAnnotation attributes to make them more friendly. It is a completely viable solution as we done this recently on two larger WPF projects and are doing the same thing currently on a third. Now with the caveat… We did find ourselves having to create a lot of custom DataAnnotation classes to handle a lot of complex business rules, so it depends on the complexity of the business rules around your data. If you are primarily able to support validation in your app using mostly out-of-the-box DataAnnotations then you would not face this same challenge. As far as error messages go, you can set the named property on the DataAnnotation attribute like:

        [Required(ErrorMessage = "Title is required.")]
    

    So you can pretty up messages for properties named like “FirstName” you can display in the message as “First Name is required…”.

    Also, as long as you have done the work to wire everything up in your base class to IDataErrorInfo then you are golden in a WPF app. It does make your code much cleaner and minimizes what you have to write (except for the custom validation attributes if you need them).

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

Sidebar

Related Questions

So I am working through my first WPF project and I am liking what
I am working on a WPF project where I have to import data from
I am working on a WPF project, and right now I am validating data
I have been working through a very small-scale WPF project in order to familiarize
I would like to use an MVVM in a WPF project I'm working on,
I'm working on a WPF project using MVVM and I'm trying to implement a
I am working on a WPF project. And I just created a dependency property.
I am working on a WPF project, and I am trying to fire an
I am working on a WPF project, using MVVM and Microsoft Prism libraries .
I'm working on a multi-lingual WPF project that will be localized into many different

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.