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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:43:35+00:00 2026-05-13T12:43:35+00:00

I have validation hooked up to a model that is bound to the TextBox

  • 0

I have validation hooked up to a model that is bound to the TextBox container. When the window is first opened validation errors appear as the model is empty, I do not want to see validation errors until submit of the window or the text in the TextBox has changed or on lost focus.

Here is the TextBox:

<TextBox Text="{Binding 
                   Path=Firstname, 
                   UpdateSourceTrigger=PropertyChanged, 
                   ValidatesOnDataErrors=True}"
         Width="124"
         Height="24"/>

How can this be achieved?

  • 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-13T12:43:36+00:00Added an answer on May 13, 2026 at 12:43 pm

    This really depends on your implementation of IDataErrorInfo. If you base it around a Dictionary of error messages you can control when validation runs that adds to that list. You would normally want to do that from your property setters (like whenever you call PropertyChange), here calling CheckValidationState:

        public string this[string columnName]
        {
            get
            {
                return ValidateProperty(columnName);
            }
        }
    
        public Dictionary<string, string> Errors { get; private set; }
    
        protected void SetError(string propertyName, string errorMessage)
        {
            Debug.Assert(!String.IsNullOrEmpty(propertyName), "propertyName is null or empty.");
            if (String.IsNullOrEmpty(propertyName))
                return;
    
            if (!String.IsNullOrEmpty(errorMessage))
            {
                if (Errors.ContainsKey(propertyName))
                    Errors[propertyName] = errorMessage;
                else
                    Errors.Add(propertyName, errorMessage);
            }
            else if (Errors.ContainsKey(propertyName))
                Errors.Remove(propertyName);
    
            NotifyPropertyChanged("Errors");
            NotifyPropertyChanged("Error");
            NotifyPropertyChanged("Item[]");
        }
    
        protected virtual string ValidateProperty(string propertyName)
        {
            return Errors.ContainsKey(propertyName) ? Errors[propertyName] : null;
        }
    
        protected virtual bool CheckValidationState<T>(string propertyName, T proposedValue)
        {
            // your validation logic here
        }
    

    You can then also include a method that validates all of your properties (like during a save):

        protected bool Validate()
        {
            if (Errors.Count > 0)
                return false;
    
            bool result = true;
            foreach (PropertyInfo propertyInfo in GetType().GetProperties())
            {
                if (!CheckValidationState(propertyInfo.Name, propertyInfo.GetValue(this, null)))
                    result = false;
                NotifyPropertyChanged(propertyInfo.Name);
            }
            return result;
        }
    

    UPDATE:

    I would recommend putting the above code into a base ViewModel class so you can reuse it. You could then create a derived class like this:

    public class SampleViewModel : ViewModelBase
    {
        private string _firstName;
    
        public SampleViewModel()
        {
            Save = new DelegateCommand<object>(SaveExecuted);
        }
    
        public DelegateCommand<object> Save { get; private set; }
    
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (_firstName == value)
                    return;
    
                CheckValidationState("FirstName", value);
    
                _firstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }
    
        public void SaveExecuted(object obj)
        {
            bool isValid = Validate();
            MessageBox.Show(isValid ? "Saved" : "Validation Error. Save canceled"); // TODO: do something appropriate to your app here
        }
    
        protected override bool CheckValidationState<T>(string propertyName, T proposedValue)
        {
            // your validation logic here
            if (propertyName == "FirstName")
            {
                if (String.IsNullOrEmpty(proposedValue as String))
                {
                    SetError(propertyName, "First Name is required.");
                    return false;
                }
                else if (proposedValue.Equals("John"))
                {
                    SetError(propertyName, "\"John\" is not an allowed name.");
                    return false;
                }
                else
                {
                    SetError(propertyName, String.Empty); // clear the error
                    return true;
                }
            }
            return true;
        }
    }
    

    In this case I’m using a DelegateCommand to trigger the save operation but it could be anything that makes a method call to do the saving. This setup allows for the initial empty state to show up as valid in the UI but either a change or a call to Save updates the validation state. You can also get a lot more general and more complicated in the way you actually do the validation so it doesn’t all end up in one method (here with some assumptions about the type) but this is simplified to make it easier to start with.

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

Sidebar

Related Questions

I have a group of text boxes that have required field validation hooked up
I have a validation message self.errors.add_to_base(_(country cannot be deleted #{self.country_name})) this is not working.
I recently inherited a VBA macro that needs to have validation logic added to
I have a validation control that has the following expression: (?=(.*\\d.*){2,})(?=(.*\\w.*){2,})(?=(.*\\W.*){1,}).{8,} That's a password
I have a Stored Proc that do a validation on a parameter ex. IF
I have a form submit button that has asp.net validators hooked up to it.
I have a custom validation function in JavaScript in a user control on a
In good old MFC, the DDX routines would have built in validation for form
I have some client-side validation against a text box, which only allows numbers up
I have been seriously disappointed with WPF validation system. Anyway! How can I validate

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.