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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:25:18+00:00 2026-05-23T11:25:18+00:00

How to disable/enable a button while doing validation using IDataErrorInfo ? I am using

  • 0

How to disable/enable a button while doing validation using IDataErrorInfo?

I am using MVVM using GalaSoft light Framework. In my Model class I have implemented IDataErrorInfo to display the error messages.

public string this[string columnName]
{
    get
    {
        Result = null;
        if (columnName == "FirstName")
        {
            if (String.IsNullOrEmpty(FirstName))
            {
                Result = "Please enter first name";
            }
        }
        else if (columnName == "LastName")
        {
            if (String.IsNullOrEmpty(LastName))
            {
                Result = "Please enter last name";
            }
        }

        else if (columnName == "Address")
        {
            if (String.IsNullOrEmpty(Address))
            {
                Result = "Please enter Address";
            }
        }

        else if (columnName == "City")
        {
            if (String.IsNullOrEmpty(City))
            {
                Result = "Please enter city";
            }
        }

        else if (columnName == "State")
        {
            if (State == "Select")
            {
                Result = "Please select state";
            }
        }

        else if (columnName == "Zip")
        {
            if (String.IsNullOrEmpty(Zip))
            {
                Result = "Please enter zip";

            }
            else if (Zip.Length < 6)
            {
                Result = "Zip's length has to be at least 6 digits!";

            }
            else
            {
                bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");

                if (zipNumber == false)
                {
                    Result = "Please enter only digits in zip";


                }
            }
        }
        else if (columnName == "IsValid")
        {
            Result = true.ToString();
        }

        return Result;

    }
}

Screenshot: https://i.stack.imgur.com/kwEI8.jpg

How to disable/enable save button. Kindly suggest?

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-05-23T11:25:18+00:00Added an answer on May 23, 2026 at 11:25 am

    The Josh Smith Way of doing this is to create the following methods in the Model:

    static readonly string[] ValidatedProperties =
    {
        "Foo",
        "Bar"
    };
    
    /// <summary>
    /// Returns true if this object has no validation errors.
    /// </summary>
    public bool IsValid
    {
        get
        {
            foreach (string property in ValidatedProperties)
            {
    
                if (GetValidationError(property) != null) // there is an error
                    return false;
            }
    
            return true;
        }
    }
    
    private string GetValidationError(string propertyName)
    {
        string error = null;
    
        switch (propertyName)
        {
            case "Foo":
                error = this.ValidateFoo();
                break;
    
            case "Bar":
                error = this.ValidateBar();
                break;
    
            default:
                error = null;
                throw new Exception("Unexpected property being validated on Service");
        }
    
        return error;
    }
    

    The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:

    /// <summary>
    /// Checks if all parameters on the Model are valid and ready to be saved
    /// </summary>
    protected bool CanSave
    {
        get
        {
            return modelOfThisVM.IsValid;
        }
    }
    

    Finally, if you are using RelayCommand, you can set the predicate of the command to the CanSave property, and the View will automatically enable or disable the button. In the ViewModel:

    /// <summary>
    /// Saves changes Command
    /// </summary>
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
                _saveCommand = new RelayCommand(param => this.SaveChanges(), param => this.CanSave);
    
            return _saveCommand;
        }
    }
    

    And in the View:

    <Button Content="Save" Command="{Binding Path=SaveCommand}"/>
    

    And that’s it!

    PS: If you haven’t read Josh Smith’s article yet, it will change your life.

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

Sidebar

Related Questions

I have a form, and want to disable/enable its submit button depending on whether
I have a Button on appwidget, that I need to 'enable'/'disable' programmatically from a
I just want to enable or disable the button inside a ControlTemplate for a
I see that you can enable/disable using the EnableWindow method, but how do I
Im am trying to add a check box that will enable/disable the edit button.
can any one tell me how can we enable/ disable button by radio button
I was wondering how you can disable button (not submit) with jQuery while a
I have this sample code for disable/enable WIFI in Windows CE. [DllImport(coredll.dll, SetLastError =
I want to disable/enable a button with JavaScript. Because the JavaScript is called, after
I need to Enable/Disable my SAVE button in real time based on data in

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.