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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:44:37+00:00 2026-06-12T23:44:37+00:00

I have a model: public class Product { public int Rating { get; set;

  • 0

I have a model:

public class Product
{
    public int Rating { get; set; }
    ...
}

and a View Model:

public class ProductViewModel: IDataErrorProvider
{
    public int Temperature { get; set; }
    public Product CurrentProduct { get; set; }

    public string this[string columnName]
    {
        get
        {
            if (columnName == "Rating")
            {
                if (CurrentProduct.Rating > Temperature)
                    return "Rating is too high for current temperature";
            }
            return null;
        }
    }
}

My view has an instance of ProductViewModel as the DataContext. The view has the field:

<TextBox Text={Binding Path=CurrentProduct.Rating, ValidatesOnDataErrors=True} .../>

By default, validation occurs on the IDataErrorProvider of the bound object (Product), not the DataContext (ProductViewModel). So in the above instance, ProductViewModel validation is never called. This is just a simple example but illustrates the problem. The model doesn’t (and shouldn’t) know about Temperature, so the design dictates that the VM should perform the validation on that field.

Yes, I could hack it and replicate the bound properties of the model directly in the ViewModel, but I would have thought there must be an easier way to redirect the call to the VM rather than the model?

  • 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-12T23:44:37+00:00Added an answer on June 12, 2026 at 11:44 pm

    I’ve encountered that problem before, and my solution is to expose a validation delegate from my Models which is checked when validating the class, and the ViewModel can use this to hook addition validation to the class that is unrelated to Model itself

    For example, I would use code that looked something like this from the ViewModel to attach a validation delegate to the Model anytime its set

    public class ProductViewModel
    {
        public int Temperature { get; set; }
    
        private product _currentProduct;
        public Product CurrentProduct 
        { 
            get { return _currentProduct; }
            set
            {
                if (value != _currentProduct)
                {
                    if (_currentProduct != null)
                        _currentProduct.RemoveValidationDelegate(ValidateProduct);
    
                    _currentProduct = value;
    
                    if (_currentProduct != null)
                        _currentProduct.AddValidationDelegate(ValidateProduct);
    
                    RaisePropertyChanged("CurrentProduct");
                }
            }
        }
    
        // Product Validation Delegate to verify temperature
        private string ValidateProduct(object sender, string propertyName)
        {
            if (propertyName == "Rating")
            {
                if (CurrentProduct.Rating > Temperature)
                    return "Rating is too high for current temperature";
            }
    
            return null;
        }
    }
    

    The actual code that adds the ValidationDelegate to the Model is pretty generic, so I typically have it in a BaseViewModel so all Models can have this functionality without me having to type it out for each one

    #region IDataErrorInfo & Validation Members
    
    #region Validation Delegate
    
    public delegate string ValidationDelegate(
        object sender, string propertyName);
    
    private List<ValidationDelegate> _validationDelegates = new List<ValidationDelegate>();
    
    public void AddValidationDelegate(ValidationDelegate func)
    {
        _validationDelegates.Add(func);
    }
    
    public void RemoveValidationDelegate(ValidationDelegate func)
    {
        if (_validationDelegates.Contains(func))
            _validationDelegates.Remove(func);
    }
    
    #endregion // Validation Delegate
    
    #region IDataErrorInfo for binding errors
    
    string IDataErrorInfo.Error { get { return null; } }
    
    string IDataErrorInfo.this[string propertyName]
    {
        get { return this.GetValidationError(propertyName); }
    }
    
    public string GetValidationError(string propertyName)
    {
        string s = null;
    
        foreach (var func in _validationDelegates)
        {
            s = func(this, propertyName);
            if (s != null)
                return s;
        }
    
        return s;
    }
    
    #endregion // IDataErrorInfo for binding errors
    
    #endregion // IDataErrorInfo & Validation Members
    

    I also have this approach outlined in my blog post here if you want to see another example.

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

Sidebar

Related Questions

I have this model: public class Package { public string CustomerName { get; set;
I have the following model: public class Product { public int Id { get;
I have the following model: public class Tag { public int Id { get;
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have something similar to this model: public class Product { public int ID
I have the following class... public class Product { public int Id { get;
I have two model class: public class SalesItem { public string ProductName { get;
So I have the following model: public class Person { public String FirstName {
I have 2 class in Model public class User { public int UserID {
I have these classes: public class Product { [Key] public virtual int ProductId {

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.