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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:39:06+00:00 2026-05-10T17:39:06+00:00

I have been seriously disappointed with WPF validation system. Anyway! How can I validate

  • 0

I have been seriously disappointed with WPF validation system. Anyway! How can I validate the complete form by clicking the ‘button’?

For some reason everything in WPF is soo complicated! I can do the validation in 1 line of code in ASP.NET which requires like 10-20 lines of code in WPF!!

I can do this using my own ValidationEngine framework:

Customer customer = new Customer(); customer.FirstName = 'John'; customer.LastName = String.Empty;  ValidationEngine.Validate(customer);  if (customer.BrokenRules.Count > 0) {    // do something display the broken rules!  } 
  • 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. 2026-05-10T17:39:06+00:00Added an answer on May 10, 2026 at 5:39 pm

    A WPF application should disable the button to submit a form iff the entered data is not valid. You can achieve this by implementing the IDataErrorInfo interface on your business object, using Bindings with ValidatesOnDataErrors=true. For customizing the look of individual controls in the case of errors, set a Validation.ErrorTemplate.

    XAML:

    <Window x:Class='Example.CustomerWindow' ...>     <Window.CommandBindings>         <CommandBinding Command='ApplicationCommands.Save'                         CanExecute='SaveCanExecute'                         Executed='SaveExecuted' />     </Window.CommandBindings>     <StackPanel>         <TextBox Text='{Binding FirstName, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}' />         <TextBox Text='{Binding LastName, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}' />         <Button Command='ApplicationCommands.Save' IsDefault='True'>Save</Button>         <TextBlock Text='{Binding Error}'/>     </StackPanel> </Window> 

    This creates a Window with two TextBoxes where you can edit the first and last name of a customer. The ‘Save’ button is only enabled if no validation errors have occurred. The TextBlock beneath the button shows the current errors, so the user knows what’s up.

    The default ErrorTemplate is a thin red border around the erroneous Control. If that doesn’t fit into you visual concept, look at Validation in Windows Presentation Foundation article on CodeProject for an in-depth look into what can be done about that.

    To get the window to actually work, there has to be a bit infrastructure in the Window and the Customer.

    Code Behind

    // The CustomerWindow class receives the Customer to display // and manages the Save command public class CustomerWindow : Window {     private Customer CurrentCustomer;     public CustomerWindow(Customer c)      {         // store the customer for the bindings         DataContext = CurrentCustomer = c;         InitializeComponent();     }      private void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e)     {         e.CanExecute = ValidationEngine.Validate(CurrentCustomer);     }      private void SaveExecuted(object sender, ExecutedRoutedEventArgs e)      {         CurrentCustomer.Save();     } }  public class Customer : IDataErrorInfo, INotifyPropertyChanged {     // holds the actual value of FirstName     private string FirstNameBackingStore;     // the accessor for FirstName. Only accepts valid values.     public string FirstName {         get { return FirstNameBackingStore; }         set {             FirstNameBackingStore = value;             ValidationEngine.Validate(this);             OnPropertyChanged('FirstName');         }     }     // similar for LastName              string IDataErrorInfo.Error {         get { return String.Join('\n', BrokenRules.Values); }     }      string IDataErrorInfo.this[string columnName]     {         get { return BrokenRules[columnName]; }     } } 

    An obvious improvement would be to move the IDataErrorInfo implementation up the class hierarchy, since it only depends on the ValidationEngine, but not the business object.

    While this is indeed more code than the simple example you provided, it also has quite a bit more of functionality than only checking for validity. This gives you fine grained, and automatically updated indications to the user about validation problems and automatically disables the ‘Save’ button as long as the user tries to enter invalid data.

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

Sidebar

Related Questions

Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been using TortoiseSVN for some time and I really like it. I
Sorry guys, I hate asking dumb questions but I have seriously been searching for
Seriously i've been looking for weeks, even months for some serious help with OpenGl
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close
Have been reading about async and tasks and been attempting to convert the CopyFileEx
Have been searching how to convert a dictionary to a string. But the results
Have have been trying to make a validator for my xml files. I have
I have been tracking emails for years using a "beacon" image and for those

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.