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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:45:35+00:00 2026-06-14T21:45:35+00:00

As you can likely see from the title, I am about to ask something

  • 0

As you can likely see from the title, I am about to ask something which has been asked many times before. But still, after reading all these other questions, I cannot find a decent solution to my problem.

I have a model class with basic validation:

partial class Player : IDataErrorInfo
{
    public bool CanSave { get; set; }

    public string this[string columnName]
    {
        get 
        { 
            string result = null;
            if (columnName == "Firstname")
            {
                if (String.IsNullOrWhiteSpace(Firstname))
                {
                    result = "Geef een voornaam in";
                }
            }
            if (columnName == "Lastname")
            {
                if (String.IsNullOrWhiteSpace(Lastname))
                {
                    result = "Geef een familienaam in";
                }
            }
            if (columnName == "Email")
            {
                try
                {
                    MailAddress email = new MailAddress(Email);
                }
                catch (FormatException)
                {
                    result = "Geef een geldig e-mailadres in";
                }
            }
            if (columnName == "Birthdate")
            {
                if (Birthdate.Value.Date >= DateTime.Now.Date)
                {
                    result = "Geef een geldige geboortedatum in";
                }
            }

            CanSave = true; // this line is wrong
            return result;
        }
    }

    public string Error { get { throw new NotImplementedException();} }
}

This validation is done everytime the property changes (so everytime the user types a character in the textbox):

<TextBox Text="{Binding CurrentPlayer.Firstname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="137" IsEnabled="{Binding Editing}" Grid.Row="1"/>

This works perfect. The validation occurs (the PropertyChanged code for the binding is done in the VM on the CurrentPlayer property, which is an object of Player).

What I would like to do now is disable the save button when the validation fails.

First of all, the easiest solutions seems to be found in this thread:
Enable Disable save button during Validation using IDataErrorInfo

  1. If I want to follow the accepted solution, I’d have to write my
    validation code twice, as I cannot simply use the indexer. Writing
    double code is absolutely not what I want, so that’s not a solution
    to my problem.
  2. The second answer on that thread sounded very promising as first,
    but the problem is that I have multiple fields that have to be
    validated. That way, everything relies on the last checked property
    (so if that field is filled in correctly, CanSave will be true, even
    though there are other fields which are still invalid).

One more solution I’ve found is using an ErrorCount property. But as I’m validating at each property change (and so at each typed character), this isn’t possible too – how could I know when to increase/decrease the ErrorCount?

What would be the best way to solve this problem?

Thanks

  • 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-14T21:45:36+00:00Added an answer on June 14, 2026 at 9:45 pm

    I’ve implemented the map approach shown in my comment above, in C# this is called a Dictionary in which I am using anonymous methods to do the validation:

    partial class Player : IDataErrorInfo
    {
        private delegate string Validation(string value);
        private Dictionary<string, Validation> columnValidations;
        public List<string> Errors;
    
        public Player()
        {
            columnValidations = new Dictionary<string, Validation>();
            columnValidations["Firstname"] = delegate (string value) {
                return String.IsNullOrWhiteSpace(Firstname) ? "Geef een voornaam in" : null;
            }; // Add the others...
    
            errors = new List<string>();
        }
    
        public bool CanSave { get { return Errors.Count == 0; } }
    
        public string this[string columnName]
        {
            get { return this.GetProperty(columnName); } 
    
            set
            { 
                var error = columnValidations[columnName](value);
    
                if (String.IsNullOrWhiteSpace(error))
                    errors.Add(error);
                else
                    this.SetProperty(columnName, value);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the tagging stuff has been mentioned many times, but I can't seem
So I'm far from an expert on C, but something's been bugging me about
I've been wondering about this for a while and I can't see why Google
I must be missing something simple, but I can't see it. First, the setup:
I'm sure that this is likely a simple solution, but I can't see my
Can the Sessionid from a given IIS server repeat after a reboot? How likely
Note These URL's are likely not available. Facebook has been updated a number of
While HTML Scraping is pretty well-documented from what I can see, and I understand
I am likely missing something simple but I can't really figure it out. This
I've done a good search to see if anyone else has asked this question,

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.