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 6345241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:47:06+00:00 2026-05-24T20:47:06+00:00

I am writing a WPF application and I want to use Data Annotations to

  • 0

I am writing a WPF application and I want to use Data Annotations to specify things like Required Fields, Range, etc.

My ViewModel classes use the regular INotifyPropertyChanged interface and I can validate the entire object easily enough using the C# 4 Validator, but I would also like the fields to highlight red if they do not validate properly. I found this blog post here (http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/28/wpf-data-validation-using-net-data-annotations-part-ii.aspx) that talks about how to write your base view model to implement IDataErrorInfo and simply use the Validator, but the implementation doesn’t actually compile nor can I see how it would work. The method in question is this:

    /// <summary>
    /// Validates current instance properties using Data Annotations.
    /// </summary>
    /// <param name="propertyName">This instance property to validate.</param>
    /// <returns>Relevant error string on validation failure or <see cref="System.String.Empty"/> on validation success.</returns>
    protected virtual string OnValidate(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            throw new ArgumentException("Invalid property name", propertyName);
        }

        string error = string.Empty;
        var value = GetValue(propertyName);
        var results = new List<ValidationResult>(1);
        var result = Validator.TryValidateProperty(
            value,
            new ValidationContext(this, null, null)
            {
                MemberName = propertyName
            },
            results);

        if (!result)
        {
            var validationResult = results.First();
            error = validationResult.ErrorMessage;
        }

        return error;
    }

The problem is GetValue is not provided. He could be talking about the GetValue that comes when you inherit DependencyObject, but the syntax still doesn’t work (it expects you to pass DependencyProperty as a parameter) but I’m using regular CLR properties with OnPropertyChanged("MyProperty") being invoked on the setter.

Is there a good way to connect the validation to the IDataErrorInfo interface?

  • 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-24T20:47:07+00:00Added an answer on May 24, 2026 at 8:47 pm

    Using your above code as a starting point I got this working through IDataErrorInfo.

    Your problem centred around getting the value of the property when you only have the property name, reflection can help here.

    public string this[string property]
    {
       get
       {
          PropertyInfo propertyInfo = this.GetType().GetProperty(property);
          var results = new List<ValidationResult>();
    
          var result = Validator.TryValidateProperty(
                                    propertyInfo.GetValue(this, null),
                                    new ValidationContext(this, null, null)
                                    {
                                      MemberName = property
                                    }, 
                                    results);
    
          if (!result)
          {
            var validationResult = results.First();
            return validationResult.ErrorMessage;
          }
    
          return string.Empty;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing a WPF application using MVVM, I want to use a WCF service,
I am writing a wpf destop application, and would like to use SQL Server
I'm writing a WPF application with C# as the code behind and I want
I'm writing an application in WPF using the MVVM-pattern and will really often use
Im writing WPF application and want to add ability to call jump list and
I am writing a WPF application, and one feature I want to implement is
I'm writing a WPF application in which i want to display the currency symbol
Im writing a WPF application that has a zoom and pan ability, but what
I am writing a WPF application on WinXP and I have overridden the default
I'm writing a WPF application and trying to bind an image to my view

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.