Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Validation Errors
    text
    copied!<p>In my current projet, I have to deal with data validation in a WPF form. My form is in a DataTemplate in a ResourceDictionnary. I can save and load the data from my form thanks to two buttons, which serialize and deserialize the data (through two <a href="http://msdn.microsoft.com/en-us/library/gg431410%28v=pandp.40%29.aspx" rel="nofollow noreferrer">DelegateCommand</a>).</p> <p>If one field of my form is empty or invalid, the save button is disable. A field is checked everytime it changes thanks to the UpdateSourceTrigger propertie. That's why I need to know in my C# code if a field is invalid to update my save command.</p> <p>Currently, I use the ExceptionValidationRule in my XAML Binding and I wonder if it's a good pratice. I can't implement a ValidationRule because I need to know in my C# code if a field is invalid, to update the save command (enable or disable the save button).</p> <pre><code>&lt;TextBox&gt; &lt;Binding Path="Contact.FirstName" UpdateSourceTrigger="PropertyChanged"&gt; &lt;Binding.ValidationRules&gt; &lt;ExceptionValidationRule/&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox&gt; </code></pre> <p>In this <a href="http://www.progware.org/Blog/post/Data-Validation-in-WPF.aspx" rel="nofollow noreferrer">blog</a>, we can read :</p> <blockquote> <p>Raising exceptions in the Setters is not a very good approach since those properties are also set by code and sometimes it is ok to temporarily leave them with error values.</p> </blockquote> <p>I already read this <a href="https://stackoverflow.com/questions/127477/detecting-wpf-validation-errors">post</a> but I can't use it, my TextBox are in a DataTemplate and I can't use them in my C# code.</p> <p>So, I wonder if I should change my Data Validation and don't use the ExceptionValidationRule.</p>
 

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