<p>I have a very simple WPF application in which I am using data binding to allow editing of some custom CLR objects. I am now wanting to put some input validation in when the user clicks save. However, all the WPF books I have read don't really devote any space to this issue. I see that you can create custom ValidationRules, but I am wondering if this would be overkill for my needs.</p>

<p>So my question is this: is there a good sample application or article somewhere that demonstrates best practice for validating user input in WPF?</p>

[{'answer_id': 63754, 'author': 'rudigrobler', 'author_id': 5147, 'author_profile': 'https://Stackoverflow.com/users/5147', 'pm_score': 7, 'selected': True, 'text': '<p>I think the new preferred way might be to use IDataErrorInfo</p>\n\n<p>Read more <a href="http://blogs.msdn.com/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx" rel="noreferrer">here</a></p>\n'}, {'answer_id': 67399, 'author': 'murki', 'author_id': 5953, 'author_profile': 'https://Stackoverflow.com/users/5953', 'pm_score': 2, 'selected': False, 'text': '<p>Also check <a href="http://www.bennedik.de/2008/05/wpf-integration-for-vab-update-for.html" rel="nofollow noreferrer">this article</a>. Supposedly Microsoft released their <a href="http://blogs.msdn.com/agile/archive/2008/05/16/enterprise-library-4-0-for-visual-studio-2008-released.aspx" rel="nofollow noreferrer">Enterprise Library (v4.0)</a> from their patterns and practices where they cover the validation subject but god knows why they didn\'t included validation for WPF, so the blog post I\'m directing you to, explains what the author did to adapt it. Hope this helps!</p>\n'}, {'answer_id': 69317, 'author': 'Greg', 'author_id': 11013, 'author_profile': 'https://Stackoverflow.com/users/11013', 'pm_score': 3, 'selected': False, 'text': '<p>personaly, i\'m using exceptions to handle validation. it requires following steps:</p>\n\n<ol>\n<li>in your data binding expression, you need to add "ValidatesOnException=True"</li>\n<li>in you data object you are binding to, you need to add DependencyPropertyChanged handler where you check if new value fulfills your conditions - if not - you restore to the object old value (if you need to) and you throw exception. </li>\n<li>in your control template you use for displaying invalid value in the control, you can access Error collection and display exception message.</li>\n</ol>\n\n<p>the trick here, is to bind only to objects which derive from DependencyObject. simple implementation of INotifyPropertyChanged wouldn\'t work - there is a bug in the framework, which prevents you from accessing error collection. </p>\n'}, {'answer_id': 3175399, 'author': 'jbe', 'author_id': 103988, 'author_profile': 'https://Stackoverflow.com/users/103988', 'pm_score': 2, 'selected': False, 'text': '<p>You might be interested in the <strong>BookLibrary</strong> sample application of the <strong><a href="http://waf.codeplex.com" rel="nofollow noreferrer">WPF Application Framework (WAF)</a></strong>. It shows how to use validation in WPF and how to control the Save button when validation errors exists.</p>\n'}, {'answer_id': 4326819, 'author': 'Pat', 'author_id': 116891, 'author_profile': 'https://Stackoverflow.com/users/116891', 'pm_score': 5, 'selected': False, 'text': '<p>From MS\'s <a href="http://msdn.microsoft.com/en-us/library/gg405484(v=PandP.40).aspx" rel="noreferrer">Patterns &amp; Practices documentation</a>:</p>\n\n<blockquote>\n  <h2>Data Validation and Error Reporting</h2>\n  \n  <p>Your view model or model will often be\n  required to perform data validation\n  and to signal any data validation\n  errors to the view so that the user\n  can act to correct them.</p>\n  \n  <p>Silverlight and WPF provide support\n  for managing data validation errors\n  that occur when changing individual\n  properties that are bound to controls\n  in the view. For single properties\n  that are data-bound to a control, the\n  view model or model can signal a data\n  validation error within the property\n  setter by rejecting an incoming bad\n  value and throwing an exception. If\n  the ValidatesOnExceptions property on\n  the data binding is true, the data\n  binding engine in WPF and Silverlight\n  will handle the exception and display\n  a visual cue to the user that there is\n  a data validation error.</p>\n  \n  <p>However, throwing exceptions with\n  properties in this way should be\n  avoided where possible. An alternative\n  approach is to implement the\n  IDataErrorInfo or INotifyDataErrorInfo\n  interfaces on your view model or model\n  classes. These interfaces allow your\n  view model or model to perform data\n  validation for one or more property\n  values and to return an error message\n  to the view so that the user can be\n  notified of the error.</p>\n</blockquote>\n\n<p>The documentation goes on to explain how to implement IDataErrorInfo and INotifyDataErrorInfo.</p>\n'}, {'answer_id': 11645396, 'author': 'Alex Pollan', 'author_id': 1503478, 'author_profile': 'https://Stackoverflow.com/users/1503478', 'pm_score': 0, 'selected': False, 'text': '<p>If your business class is directly used by your UI is preferrable to use IDataErrorInfo because it put logic closer to their owner.</p>\n\n<p>If your business class is a stub class created by a reference to an WCF/XmlWeb service then you can not/must not use IDataErrorInfo nor throw Exception for use with ExceptionValidationRule. Instead you can:</p>\n\n<ul>\n<li>Use custom ValidationRule.</li>\n<li>Define a partial class in your WPF UI project and implements IDataErrorInfo.</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/63646', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7532/']