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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:56:34+00:00 2026-05-25T09:56:34+00:00

I am using MVVM-Light and for every view model I have to create an

  • 0

I am using MVVM-Light and for every view model I have to create an implementation of the INotifyDataErrorInfo which in some cases uses same methods to validate same property types.
In this example I am using DateTime:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using SL.Resources;

    namespace SL.ViewModel
    {
        public partial class AdministrationViewModel : INotifyDataErrorInfo
        {
            #region Validations

        public void isDateToValid(DateTime? value, DateTime? dateFrom, string propertyName)
        {
            //check if null
            if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
            else RemoveError(propertyName, CommonErrors.DateNull_ERROR);

            //check if min and max value
            if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
            else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);

            if (value < dateFrom) AddError(propertyName, CommonErrors.DateFromSmall_ERROR, false);
            else RemoveError(propertyName, CommonErrors.DateFromSmall_ERROR);
        }

        public void IsDateValid(DateTime? value, string propertyName)
        {
            if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
            else RemoveError(propertyName, CommonErrors.DateNull_ERROR);

            if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
            else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);
        }

        #endregion

        #region INotifyDataErrorInfo Members

        public Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();

        // Adds the specified error to the errors collection if it is not 
        // already present, inserting it in the first position if isWarning is 
        // false. Raises the ErrorsChanged event if the collection changes. 
        public void AddError(string propertyName, string error, bool isWarning)
        {
            if (!errors.ContainsKey(propertyName))
                errors[propertyName] = new List<string>();

            if (!errors[propertyName].Contains(error))
            {
                if (isWarning) errors[propertyName].Add(error);
                else errors[propertyName].Insert(0, error);
                RaiseErrorsChanged(propertyName);
            }
        }

        // Removes the specified error from the errors collection if it is
        // present. Raises the ErrorsChanged event if the collection changes.
        public void RemoveError(string propertyName, string error)
        {
            if (errors.ContainsKey(propertyName) &&
                errors[propertyName].Contains(error))
            {
                errors[propertyName].Remove(error);
                if (errors[propertyName].Count == 0) errors.Remove(propertyName);
                RaiseErrorsChanged(propertyName);
            }
        }

        public void RaiseErrorsChanged(string propertyName)
        {
            if (ErrorsChanged != null)
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
        }

        public event System.EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

        public System.Collections.IEnumerable GetErrors(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName) ||
                !errors.ContainsKey(propertyName)) return null;
            return errors[propertyName];
        }

        public bool HasErrors
        {
            get { return errors.Count > 0; }
        }

        #endregion
    }
}

How could I make this code reusable in other view models, so I don’t have to implement the same thing over and over again?

I created a class that implements INotifyDataErrorInfo :

public class ViewModelValidation : INotifyDataErrorInfo

But when I want to use it in my view model it doesn’t work:

public partial class AdministrationViewModel : ViewModelValidation

Error:

Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...

this is because in my main view model file I have a base class from MVVM-Light:

public partial class AdministrationViewModel : ViewModelBase

Any help resolving this is appretiated.

  • 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-25T09:56:35+00:00Added an answer on May 25, 2026 at 9:56 am

    I figured it out myself. I created a ViewModelCommon class based on ViewModelBase from MVVM-Light and added the INotifyDataErrorInfo interface to it:

    public class ViewModelCommon : ViewModelBase, INotifyDataErrorInfo
    

    Then in my view model code instead of ViewModelBase I just used my ViewModelCommon class:

    public partial class AdministrationViewModel : ViewModelCommon
    

    And it works just fine.

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

Sidebar

Related Questions

I'm using MVVM (MVVM Light Toolkit) and have a property on the view model
I am using MVVM architecture. I have a usercontrol UC as a View Model
I am using MVVM Light with SL4. My View's are resolving their View Model
I'm using MVVM Light and Prism with the view model locator pattern. I really
I'm using MVVM Light toolkit (which I love). I currently have messaging in place
I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like
Using MVVM Light, I have two WPF applications that reference a common Views library.
I'm using the MVVM Light Toolkit and I have a DataGrid bound to an
I'm currently working on an app using MVVM that needs to have some data
I am using Mvvm-Light and have been ignoring how binding in XAML really works

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.