Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx" rel="noreferrer">IDataErrorInfo</a> interface on your business object, using Bindings with <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validatesondataerrors.aspx" rel="noreferrer"><code>ValidatesOnDataErrors</code></a><code>=true</code>. For customizing the look of individual controls in the case of errors, set a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.errortemplate.aspx" rel="noreferrer"><code>Validation.ErrorTemplate</code></a>.</p> <h3>XAML:</h3> <pre><code>&lt;Window x:Class="Example.CustomerWindow" ...&gt; &lt;Window.CommandBindings&gt; &lt;CommandBinding Command="ApplicationCommands.Save" CanExecute="SaveCanExecute" Executed="SaveExecuted" /&gt; &lt;/Window.CommandBindings&gt; &lt;StackPanel&gt; &lt;TextBox Text="{Binding FirstName, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;TextBox Text="{Binding LastName, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;Button Command="ApplicationCommands.Save" IsDefault="True"&gt;Save&lt;/Button&gt; &lt;TextBlock Text="{Binding Error}"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>This creates a <code>Window</code> with two <code>TextBox</code>es 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 <code>TextBlock</code> beneath the button shows the current errors, so the user knows what's up.</p> <p>The default <code>ErrorTemplate</code> is a thin red border around the erroneous Control. If that doesn't fit into you visual concept, look at <a href="http://www.codeproject.com/KB/WPF/wpfvalidation.aspx" rel="noreferrer">Validation in Windows Presentation Foundation</a> article on CodeProject for an in-depth look into what can be done about that.</p> <p>To get the window to actually work, there has to be a bit infrastructure in the Window and the Customer.</p> <h3>Code Behind</h3> <pre><code>// 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]; } } } </code></pre> <p>An obvious improvement would be to move the <code>IDataErrorInfo</code> implementation up the class hierarchy, since it only depends on the <code>ValidationEngine</code>, but not the business object.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload