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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:39:30+00:00 2026-05-14T20:39:30+00:00

I am looking for a simple solution to the following problem: I am using

  • 0

I am looking for a simple solution to the following problem:

I am using a simple TextBox control with the Text property bound to a property in the code behind. Additionally I am using a validation rule to notify the user of malformed input.

… error display style here …

Now after entering valid data into the TextBox the user can hit a button to send the data. When clicking the button the data from the bound property UserName in the code behind is evaluated and sent.

The problem is that a user can enter valid data into the TextBox and this will be set in the property UserName. If the user then decides to change the text in the TextBox and the data becomes invalid, the setter of the property UserName is not called after the failed validation.

This means that the last valid data remains in the property UserName, while the TextBox display the invalid data with the error indicator. If the user then clicks on the button to send the data, the last valid data will be sent instead of the current TextBox content.

I know I could deactivate the button if the data is invalid and in fact I do, but the method is called in the setter of UserName. And if that is not called after a failed validation the button stays enabled.

So the question is: How do I enable calling of the property setter after a failed validation?

  • 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-14T20:39:31+00:00Added an answer on May 14, 2026 at 8:39 pm

    How I handle this in my view model classes:

    public class MyViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
       private Dictionary<string, string> _Errors = new Dictionary<string, string>();
    
       public object SomeProperty
       {
          get { return _SomeProperty; }
          set
          {
             if (value != _SomeProperty && !ValidationError("SomeProperty", value))
                _SomeProperty = value;
                OnPropertyChanged("SomeProperty");
             }
          }
       }
    
       private bool ValidationError(string propertyName, object value)
       {
          // I usually have a Dictionary<string, Func<object, string>> that maps property
          // names to validation functions; the functions return null if the property
          // is valid and an error message if not.  You can embed the validation logic
          // in the property setters, of course, but breaking them out as separate methods
          // eases testing.
          _Errors[propertyName] = _ValidationMethods[propertyName](value);
          OnPropertyChanged("IsValid");
       }
    
       public bool IsValid
       {
          get { return !(_Errors.Where(x => x.Value != null).Any()));
       }
    
       public string this[string propertyName]
       {
          get
          {
             return (_Errors.ContainsKey(propertyName))
                ? _Errors[propertyName]
                : null;
          }
       }
    }
    

    It’s a little awkward to get this all set up at first, but once you’ve done it, you have a simple and straightforward way to report validation errors to the UI (via the DataErrorValidationRule), a straightforward way to know whether any given property is valid or not (check _Errors), and an IsValid property that tells you whether or not the whole view model is valid. (Also, you can extend the IsValid property to handle the case where all the properties of the view model are valid but the view model itself is not, e.g. two mutually exclusive flags are both set.) And as long as you make them internal, the validation methods can be unit tested via NUnit or whatever.

    I should add that the above code is off the top of my head and may or may not work as written – my actual working code is in a base class and has a lot of other things baked into it that would just be confusing.

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

Sidebar

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What kind of HTML formatting are you using? If it's… May 15, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer I dont think you need a transaction for this as… May 15, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer If you have other code that could be closing the… May 15, 2026 at 9:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.