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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:05:48+00:00 2026-06-17T04:05:48+00:00

I’m having trouble trying to figure out the best way to implement IDataErrorInfo in

  • 0

I’m having trouble trying to figure out the best way to implement IDataErrorInfo in my WPF application. I have a few models, each with a lot of properties, that are being used. The ViewModel has Properties of each of these models and the View uses these properties to bind to the model. This is a reduced version of the structure I have:

public class ModelA
{
    public string PropertyA1 {get; set;}
    public string PropertyA2 {get; set;}
}

public class ModelB
{
    public string Property B1 {get; set;}
    public string Property B2 {get; set;}
}

public class ViewModel
{
    public ModelA modelA {get; set;}
    public ModelB modelB {get; set;}
}

My questions is – Where do I implement IDataErrorInfo – In the ViewModel or in the Model?
The View binds to these properties as modelA.PropertyA1 and so on so the errors are raised in the Models and not in the ViewModel which makes it necessary to implement IDataErrorInfo in the Models. However, I’ve heard that it’s a good practice to implement validation logic in the ViewModel.

I was wondering if there was a way to catch the errors in the ViewModel without writing a wrapper for each of the properties that would raise an error as there are lots of properties and writing a wrapper for each of them would be tedious.

Thanks for your help!

  • 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-17T04:05:50+00:00Added an answer on June 17, 2026 at 4:05 am

    Thank you for your help guys! Here’s how I decided to solve my problem:

    I created two base classes, one for normal models (Ones that don’t have any validations. It only implements INotifyPropertyChanged) and another for models that have validations as shown below.

    public abstract class ModelBase : INotifyPropertyChanged
    {
        //Implement INotifyPropertyChanged here
    }
    
    public delegate string ValidateProperty(string propertyName);
    
    public abstract class ValidationModelBase : ModelBase, IDataErrorInfo
    {
        private bool _canValidate;
        public bool CanValidate
        {
            get { return _canValidate; }
            set { _canValidate = value; }
        }
    
        #region IDataErrorInfo Members
    
        public string Error
        {
            get { return string.Empty; }
        }
    
        public string this[string columnName]
        {
            get
            {
                if (this.CanValidate)
                {
                    return this.Validate(columnName);
                }
                return string.Empty;
            }
        }
    
        #endregion
    
        #region Validation Section
    
        public event ValidateProperty OnValidateProperty;
    
        public string Validate(string propertyName)
        {
            if (this.OnValidateProperty != null)
            {
                return OnValidateProperty(propertyName);
            }
            return string.Empty;
        }
    
        #endregion
    }
    

    Now my models looked like this:

    public class ModelA : validationModelBase
    {
        public string PropertyA1 {get; set;}
        public string PropertyA2 {get; set;}
    }
    
    public class ModelB : ValidationModelBase
    {
        public string Property B1 {get; set;}
        public string Property B2 {get; set;}
    }
    

    Not a huge change there. The ViewModel now looks like this:

    public class ViewModel
    {
        public ModelA modelA {get; set;}
        public ModelB modelB {get; set;}
    
        public ViewModel()
        {
            this.modelA.OnValidateProperty += new ValidateProperty(ValidateModelA);
            this.modelB.OnValidateProperty += new ValidateProperty(ValidateModelB);
        }
    
        private string ValidateModelA(string propertyName)
        {
            //Implement validation logic for ModelA here
        }    
    
        private string ValidateModelB(string propertyName)
        {
            //Implement validation logic for ModelB here
        }
    }
    

    This seems to be working for me so far. This way, any new models that have validation need only derive from ValidationModelBase and have the ViewModel add an event handler for the validation event.

    If anyone has a better way of solving my problem, do let me know – I’m open to suggestions and improvements.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

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.