Custom ValidationRule throws ArgumentOutOfRangeExceptionHi  there.<br><br>I've been developing a custom ValidationRule, following the MS example by the letter but it is throwing an ArhumentOutOfRangeException during the validation process.<br><br>I have the following ValidationRule<br> <div style="text-align:left"> <div style="text-align:left"> <div style="text-align:left"><br><font size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier">public class MyValidationRule : ValidationRule</span></font></span></font><br style="font-family:Courier"><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier">{</span></font><font size=2><span style="font-family:Courier"></span></font></span></font><br><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier"></span></font> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">public override System.Windows.Controls.ValidationResult Validate(object value, CultureInfo cultureInfo)</span></font><br style="font-family:Courier"></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">{</span></font><br style="font-family:Courier"></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>...</font></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="font-family:Courier;text-align:left"><font size=2>          if ( errorCondition1 )</font><br></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"> <div style="margin-left:40px"><font size=2>return new ValidationResult(false);</font></div></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>if ( errorCondition2)</font></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"> <div style="margin-left:40px"><font size=2>return new ValidationResult(false,&quot;Error 2&quot;);</font></div></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>return ValidationResult.ValidResult;</font></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">}</span></font></div></span></font> <p></p> <p>}</p></div></div></font><br><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier"></span></font></span></font><br style="font-family:Courier"><font size=2><span style="font-family:Courier"> <div style="text-align:left"><font size=2><span style="font-family:Courier"></span></font></div></span></font></div>A custom TextBox derived control uses the validation rule, as follows. The MyTextBox.Text property is bound to the MyText property on the DataContext object<br></div></div><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font><font><font size=2><span style="width:100%"><br></span><font size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div style="text-align:left">&lt;local:MyTextBox x:Name=&quot;_myTextBox&quot; Validation.ErrorTemplate=&quot;{StaticResource TextBoxValidationTemplate}&quot; Style=&quot;{StaticResource TextBoxInError}&quot;&gt;<br></div> <div style="margin-left:40px;text-align:left">&lt;local:MyTextBox.Text&gt;<br></div> <div style="margin-left:80px;text-align:left">&lt;Binding Path=&quot;MyText&quot; UpdateSourceTrigger=&quot;PropertyChanged&quot;&gt;<br></div> <div style="margin-left:120px;text-align:left">&lt;Binding.ValidationRules&gt;<br></div> <div style="margin-left:160px;text-align:left">&lt;local:MyValidationRule/&gt;<br></div> <div style="margin-left:120px;text-align:left">&lt;/Binding.ValidationRules&gt;<br></div> <div style="margin-left:80px;text-align:left">&lt;/Binding&gt;<br></div> <div style="margin-left:40px;text-align:left">&lt;/local:MyTextBox.Text&gt;<br></div>&lt;/local:MyTextBox&gt;</div></div></font><br><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font></font></font><font size=2><span style="font-family:Courier"><span style="font-family:Arial">The TextBoxInError style is defined as follows</span><br><br></span></font> <div style="text-align:left"></div> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div style="text-align:left"><font size=2><span style="font-family:Courier">&lt;Style x:Key=&quot;TextBoxInError&quot; TargetType=&quot;{x:Type local:MyTextBox}&quot;&gt;</span></font><br> <div style="margin-left:40px"><font size=2><span style="font-family:Courier">&lt;Style.Triggers&gt;</span></font><br></div> <div style="margin-left:80px"><font size=2><span style="font-family:Courier">&lt;Trigger Property=&quot;Validation.HasError&quot; Value=&quot;true&quot;&gt;</span></font><br></div> <div style="margin-left:120px"><font size=2><span style="font-family:Courier">&lt;Setter Property=&quot;ToolTip&quot; Value=&quot;{Binding RelativeSource={x:Static RelativeSource.Self},</span></font><font size=2><span style="font-family:Courier">                                       Path=(Validation.Errors)[0].ErrorContent}&quot;/&gt;</span></font><br></div> <div style="margin-left:80px"><font size=2><span style="font-family:Courier">&lt;/Trigger&gt;</span></font><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font><br></div> <div style="margin-left:40px"><font size=2><span style="font-family:Courier">&lt;/Style.Triggers&gt;</span></font><br></div><font size=2><span style="font-family:Courier"></span></font></div>&lt;/Style&gt;<br></div></div><br> <p><br></p> <p>Validation is performed as expected, and the tooltip is correctly set. However, when the ValidationResult changes, the following error is reported</p><br> <p style="text-align:left"></p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>System.Windows.Data Error: 12 : Cannot get '' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0).[0].ErrorContent; DataItem='MyTextBox' (Name='_myTextBox'); target element is 'MyTextBox' (Name='_myTextBox'); target property is 'ToolTip' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.<br>Parameter name: index<br>   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)<br>   at System.ThrowHelper.ThrowArgumentOutOfRangeException()<br>   at System.Collections.Generic.List`1.get_Item(Int32 index)<br>   at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)<br>   at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)<br>   --- End of inner exception stack trace ---<br>   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br>   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br>   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br>   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)<br>   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)<br>   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'</p></div></div> <p></p> <p style="text-align:left">It seems that accessing ValidationErrors[0] is the cause of the exception, but I cannot see how this is occuring since that property is only accessed when an Error exists.</p><br> <p style="text-align:left"><br></p> <p style="text-align:left">Any pointers appreciated.<br></p>© 2009 Microsoft Corporation. All rights reserved.Fri, 21 Aug 2009 19:17:50 Z1c6cd214-abce-4a8b-a919-0726dd81461ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#1c6cd214-abce-4a8b-a919-0726dd81461ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#1c6cd214-abce-4a8b-a919-0726dd81461aplanetmarshallukhttp://social.msdn.microsoft.com/Profile/en-US/?user=planetmarshallukCustom ValidationRule throws ArgumentOutOfRangeExceptionHi  there.<br><br>I've been developing a custom ValidationRule, following the MS example by the letter but it is throwing an ArhumentOutOfRangeException during the validation process.<br><br>I have the following ValidationRule<br> <div style="text-align:left"> <div style="text-align:left"> <div style="text-align:left"><br><font size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier">public class MyValidationRule : ValidationRule</span></font></span></font><br style="font-family:Courier"><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier">{</span></font><font size=2><span style="font-family:Courier"></span></font></span></font><br><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier"></span></font> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">public override System.Windows.Controls.ValidationResult Validate(object value, CultureInfo cultureInfo)</span></font><br style="font-family:Courier"></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">{</span></font><br style="font-family:Courier"></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>...</font></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="font-family:Courier;text-align:left"><font size=2>          if ( errorCondition1 )</font><br></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"> <div style="margin-left:40px"><font size=2>return new ValidationResult(false);</font></div></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>if ( errorCondition2)</font></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"> <div style="margin-left:40px"><font size=2>return new ValidationResult(false,&quot;Error 2&quot;);</font></div></div></span></font><br><font size=2><span style="font-family:Courier"> <div style="margin-left:80px;font-family:Courier;text-align:left"><font size=2>return ValidationResult.ValidResult;</font></div></span></font><font size=2><span style="font-family:Courier"> <div style="margin-left:40px;text-align:left"><font size=2><span style="font-family:Courier">}</span></font></div></span></font> <p></p> <p>}</p></div></div></font><br><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"><font size=2><span style="font-family:Courier"></span></font></span></font><br style="font-family:Courier"><font size=2><span style="font-family:Courier"> <div style="text-align:left"><font size=2><span style="font-family:Courier"></span></font></div></span></font></div>A custom TextBox derived control uses the validation rule, as follows. The MyTextBox.Text property is bound to the MyText property on the DataContext object<br></div></div><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font><font><font size=2><span style="width:100%"><br></span><font size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div style="text-align:left">&lt;local:MyTextBox x:Name=&quot;_myTextBox&quot; Validation.ErrorTemplate=&quot;{StaticResource TextBoxValidationTemplate}&quot; Style=&quot;{StaticResource TextBoxInError}&quot;&gt;<br></div> <div style="margin-left:40px;text-align:left">&lt;local:MyTextBox.Text&gt;<br></div> <div style="margin-left:80px;text-align:left">&lt;Binding Path=&quot;MyText&quot; UpdateSourceTrigger=&quot;PropertyChanged&quot;&gt;<br></div> <div style="margin-left:120px;text-align:left">&lt;Binding.ValidationRules&gt;<br></div> <div style="margin-left:160px;text-align:left">&lt;local:MyValidationRule/&gt;<br></div> <div style="margin-left:120px;text-align:left">&lt;/Binding.ValidationRules&gt;<br></div> <div style="margin-left:80px;text-align:left">&lt;/Binding&gt;<br></div> <div style="margin-left:40px;text-align:left">&lt;/local:MyTextBox.Text&gt;<br></div>&lt;/local:MyTextBox&gt;</div></div></font><br><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font></font></font><font size=2><span style="font-family:Courier"><span style="font-family:Arial">The TextBoxInError style is defined as follows</span><br><br></span></font> <div style="text-align:left"></div> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div style="text-align:left"><font size=2><span style="font-family:Courier">&lt;Style x:Key=&quot;TextBoxInError&quot; TargetType=&quot;{x:Type local:MyTextBox}&quot;&gt;</span></font><br> <div style="margin-left:40px"><font size=2><span style="font-family:Courier">&lt;Style.Triggers&gt;</span></font><br></div> <div style="margin-left:80px"><font size=2><span style="font-family:Courier">&lt;Trigger Property=&quot;Validation.HasError&quot; Value=&quot;true&quot;&gt;</span></font><br></div> <div style="margin-left:120px"><font size=2><span style="font-family:Courier">&lt;Setter Property=&quot;ToolTip&quot; Value=&quot;{Binding RelativeSource={x:Static RelativeSource.Self},</span></font><font size=2><span style="font-family:Courier">                                       Path=(Validation.Errors)[0].ErrorContent}&quot;/&gt;</span></font><br></div> <div style="margin-left:80px"><font size=2><span style="font-family:Courier">&lt;/Trigger&gt;</span></font><font size=2><span style="font-family:Courier"></span></font><font size=2><span style="font-family:Courier"></span></font><br></div> <div style="margin-left:40px"><font size=2><span style="font-family:Courier">&lt;/Style.Triggers&gt;</span></font><br></div><font size=2><span style="font-family:Courier"></span></font></div>&lt;/Style&gt;<br></div></div><br> <p><br></p> <p>Validation is performed as expected, and the tooltip is correctly set. However, when the ValidationResult changes, the following error is reported</p><br> <p style="text-align:left"></p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>System.Windows.Data Error: 12 : Cannot get '' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0).[0].ErrorContent; DataItem='MyTextBox' (Name='_myTextBox'); target element is 'MyTextBox' (Name='_myTextBox'); target property is 'ToolTip' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.<br>Parameter name: index<br>   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)<br>   at System.ThrowHelper.ThrowArgumentOutOfRangeException()<br>   at System.Collections.Generic.List`1.get_Item(Int32 index)<br>   at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)<br>   at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)<br>   --- End of inner exception stack trace ---<br>   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br>   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)<br>   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br>   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)<br>   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)<br>   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'</p></div></div> <p></p> <p style="text-align:left">It seems that accessing ValidationErrors[0] is the cause of the exception, but I cannot see how this is occuring since that property is only accessed when an Error exists.</p><br> <p style="text-align:left"><br></p> <p style="text-align:left">Any pointers appreciated.<br></p>Fri, 17 Aug 2007 09:49:47 Z2007-08-20T08:30:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#7ecbb4fb-3a50-4514-9f88-f519ce6708f5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#7ecbb4fb-3a50-4514-9f88-f519ce6708f5Sam Bent - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Sam%20Bent%20-%20MSFTCustom ValidationRule throws ArgumentOutOfRangeExceptionThis is a known bug.  Sorry, I don't have a workaround offhand. <p align=left><font face=Arial size=2></font> </p>Fri, 17 Aug 2007 17:58:55 Z2007-08-17T17:58:55Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#ad4c2a22-57eb-4ec6-98c1-5b72abe8c177http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#ad4c2a22-57eb-4ec6-98c1-5b72abe8c177planetmarshallukhttp://social.msdn.microsoft.com/Profile/en-US/?user=planetmarshallukCustom ValidationRule throws ArgumentOutOfRangeExceptionWell at least I know I'm not doing anything stupid. I'll hope for a bug fix in the next service pack <img height=19 alt=Smile src="http://forums.microsoft.com/MSDN/emoticons/emotion-1.gif" width=19><br><br>Thanks,<br>Andrew.<br>Mon, 20 Aug 2007 08:30:31 Z2007-08-20T08:30:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#33206543-1c69-4a48-b7a8-9187d495ce55http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#33206543-1c69-4a48-b7a8-9187d495ce55Sam Bent - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Sam%20Bent%20-%20MSFTCustom ValidationRule throws ArgumentOutOfRangeExceptionI'm afraid you'll be disappointed.  We've known about this one for a while, but there were no reports from customers (until yours) so it was deemed low-priority.  So now it's too late for the next service pack;  best you can hope for is the one after that.  Sorry.Mon, 20 Aug 2007 17:57:04 Z2007-08-20T17:57:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#a70a73b1-8174-42f8-9f03-6bfc778e6f3bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#a70a73b1-8174-42f8-9f03-6bfc778e6f3bMartin Rasmussonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Martin%20RasmussonCustom ValidationRule throws ArgumentOutOfRangeException<p align=left><font face=Arial size=2></font> </p> <p>This is very unfortunate news, since he certainly is not the only one with these problems. </p>Tue, 30 Oct 2007 12:59:53 Z2007-10-30T12:59:53Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#d0268a19-04b8-47a3-bfa4-deed4319a69chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#d0268a19-04b8-47a3-bfa4-deed4319a69cScott Reedhttp://social.msdn.microsoft.com/Profile/en-US/?user=Scott%20ReedCustom ValidationRule throws ArgumentOutOfRangeException<p align=left><font face=Arial size=2></font> </p> <p>Sam,</p> <p align=left>Can you give us more information about the bug?  Are there any updates as to when it will be fixed?</p> <p align=left>I am having the same problem, but without the custom validation rule.  I am using a simple ExceptionValidationRule, yet my tooltip is being populated with a TargetInvocationException.</p> <p align=left> </p> <p align=left>The weird thing is, I downloaded the PaulStovell Validation code, and that seems to work fine.  So there has to be a workaround of some type?</p> <p align=left> </p> <p align=left>-Scott</p>Thu, 29 Nov 2007 07:06:40 Z2007-11-29T07:06:40Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#47c086f0-15cb-4fd6-9784-d053659b00f3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#47c086f0-15cb-4fd6-9784-d053659b00f3Sam Bent - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Sam%20Bent%20-%20MSFTCustom ValidationRule throws ArgumentOutOfRangeException<p align=left><font face=Arial size=2>Warning: this gets technical.</font></p> <p align=left> </p> <p align=left>It all depends on how the binding writes the new value back to the source property.  Sometimes it uses a PropertyInfo, calling   propertyInfo.SetValue(item, value),  and sometimes it uses a PropertyDescriptor, calling  propertyDescriptor.SetValue(item, value).  Both cases eventually call the source property's setter, which might throw an exception.  In the former case the exception bubbles up directly to the binding code, where it is caught and placed in a ValidationError object.  In the latter case, the PropertyDescriptor code catches the exception and wraps it in a TargetInvocationException, which is what the binding code catches and places in the ValidationError.</p> <p align=left> </p> <p align=left>The choice between PropertyInfo and PropertyDescriptor is complicated, and is somewhat of an implementation detail (i.e. nothing I'm about to say should be taken as a contract).  Roughly speaking, if the source object has a public setter for the property, and also implements INotifyPropertyChanged, we choose PropertyInfo.  Otherwise we choose PropertyDescriptor.  (It's actually more complicated, because we do something different if the source property is a DependencyProperty, we have to support synthetic properties implemented via ICustomTypeDescriptor or TypeDescriptorProvider, etc.)</p> <p align=left> </p> <p align=left>Chances are that Stovell's validation code is changing something that influences the choice - perhaps adding INotifyPropertyChanged - and thus removing PropertyDescriptor from the equation.</p> <p align=left> </p> <p align=left>The bug is on my radar to fix for the next release after Orcas, but it's competing with a lot of other stuff so there's no guarantee.  There's a non-trivial compat question to be answered:  if we drill into the TargetInvocationException to fill the ValidationError with the original exception, will we break existing apps that expect to see TargetInvocationException?  If anyone owns such an app, I'd appreciate hearing about it.</p>Thu, 29 Nov 2007 17:47:00 Z2007-11-29T17:47:00Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#b6c3893d-adeb-4e9b-8414-f1c219e7a991http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#b6c3893d-adeb-4e9b-8414-f1c219e7a991Pixe1grasshttp://social.msdn.microsoft.com/Profile/en-US/?user=Pixe1grassCustom ValidationRule throws ArgumentOutOfRangeException<font size=2><span style="font-family:Arial">I have noticed a large performance hit when the validation starts spitting out exceptions,<br>I'm rather disappointed to see this has not been addressed in almost a year now. <br><br>However this is the fix I came up with :<br><br>Change :<br></span></font> <div style="text-align:left"><font size=2><span id="_ctl0_MainContent_PostFlatView"><span><span style="font-family:Courier"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>&lt;Setter Property=&quot;ToolTip&quot; Value=&quot;{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}&quot;/&gt;</p></div></div></span><span style="font-family:Courier"></span></span></span></font><br></div><font size=2><br>To:<br></font> <div style="text-align:left"><font size=2> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p>&lt;Setter Property=&quot;ToolTip&quot;  Value=&quot;{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource ErrorsToString}}&quot; /&gt;</p></div></div><br></font><br></div><font size=2><br>Passing the &quot;validation.errors&quot; read only collection to a converter <br><br>(converter code is simply a check to make sure there is an array object before attempting to access it)<br><br></font><br>Thu, 17 Apr 2008 20:58:41 Z2008-04-17T20:58:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#8c0a5bd4-d8a3-430c-bcc3-e99efbb2b240http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#8c0a5bd4-d8a3-430c-bcc3-e99efbb2b240HappyNomadhttp://social.msdn.microsoft.com/Profile/en-US/?user=HappyNomadCustom ValidationRule throws ArgumentOutOfRangeExceptionHere's the code I wrote for the workaround converter:<br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> ErrorsToStringConverter : IValueConverter { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">object</font><font style="font-size:11px"> Convert( </font><font style="color:blue">object</font><font style="font-size:11px"> value, Type targetType, </font><font style="color:blue">object</font><font style="font-size:11px"> parameter, System.Globalization.CultureInfo culture ) { </font></td></tr><tr><td>        IList&lt;ValidationError&gt; validationErrors = (IList&lt;ValidationError&gt;)value; </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">return</font><font style="font-size:11px"> validationErrors.Count == 0 ? </font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px"> : validationErrors[0].ErrorContent; </font></td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">object</font><font style="font-size:11px"> ConvertBack( </font><font style="color:blue">object</font><font style="font-size:11px"> value, Type targetType, </font><font style="color:blue">object</font><font style="font-size:11px"> parameter, System.Globalization.CultureInfo culture ) { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> Exception( </font><font style="color:blue">&quot;The method or operation is not implemented.&quot;</font><font style="font-size:11px"> ); </font></td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">} </td></tr><tr><td> </td></tr></tbody></table></div>I'm also disappointed this bug hasn't been fixed.  Could someone post the link to this issue's location on Microsoft Connect?<br><br> Tue, 09 Sep 2008 05:09:36 Z2008-09-09T05:09:36Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#e67bb5e4-6b3b-4510-b5bf-84eddc55dfc2http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#e67bb5e4-6b3b-4510-b5bf-84eddc55dfc2Jenkishttp://social.msdn.microsoft.com/Profile/en-US/?user=JenkisCustom ValidationRule throws ArgumentOutOfRangeExceptionI have an application where this problem arises.<br><br>I am using LinqToSQL, which implements INotifyPropertyChanging, INotifyPropertyChanged.<br><br>I am implementing some validation logic in partial methods.  eg <br><br> partial void OnNoOfTanksChanging(int value)<br>        {<br>            if (value &gt; 2)<br>            {<br>                throw new ApplicationException(&quot;Value must be &lt;= 2&quot;);<br>            }<br>        }<br><br>I have the following :<br><br> &lt;ResourceDictionary&gt;<br>          &lt;local:ErrorsToString x:Key=&quot;ErrorsToStringConverter&quot; &gt;&lt;/local:ErrorsToString&gt;<br>        &lt;/ResourceDictionary&gt;<br><br><br>&lt;Style x:Key=&quot;TextBoxStyle&quot; TargetType=&quot;{x:Type TextBox}&quot;&gt;<br>.....<br><br> &lt;Style.Triggers&gt;<br>          &lt;Trigger Property=&quot;Validation.HasError&quot; Value=&quot;true&quot;&gt;<br>            &lt;Setter Property=&quot;ToolTip&quot;  <br>                    Value=&quot;{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), <br>                    Converter={StaticResource ErrorsToStringConverter}}&quot; /&gt;<br>          &lt;/Trigger&gt;<br>        &lt;/Style.Triggers&gt;<br><br>public class ErrorsToString : IValueConverter<br>    {<br>        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br>        {<br>            IList&lt;ValidationError&gt; validationErrors = (IList&lt;ValidationError&gt;)value;<br>            return validationErrors.Count == 0 ? &quot;&quot; : validationErrors[0].ErrorContent;<br>        }<br><br>        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br>        {<br>            throw new Exception(&quot;The method or operation is not implemented.&quot;);<br>        }<br>    } <br>}<br><br><br><br> Tue, 28 Oct 2008 06:58:52 Z2008-10-28T06:58:52Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#e697d45e-6dad-47e8-b91c-bfc99c142a1bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#e697d45e-6dad-47e8-b91c-bfc99c142a1ba_romanbhttp://social.msdn.microsoft.com/Profile/en-US/?user=a_romanbCustom ValidationRule throws ArgumentOutOfRangeExceptionYou can just use:<br><br><font size=2><span style="font-family:Courier">Path=(Validation.Errors).CurrentItem.ErrorContent</span></font><br><br>That works fine for me.<br> Wed, 11 Mar 2009 21:07:54 Z2009-03-11T21:07:54Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#ab7922bf-d1b4-4761-a472-f1f4d753349ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#ab7922bf-d1b4-4761-a472-f1f4d753349eausadminhttp://social.msdn.microsoft.com/Profile/en-US/?user=ausadminCustom ValidationRule throws ArgumentOutOfRangeExceptionI have also encountered this problem. I changed my xaml code as suggested by a_romanb:<br/><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small">ToolTip </span></span></span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">=&quot;{</span></span><span style="color:#a31515;font-size:x-small"><span style="color:#a31515;font-size:x-small">Binding</span></span><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small"> RelativeSource</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">={</span></span><span style="color:#a31515;font-size:x-small"><span style="color:#a31515;font-size:x-small">RelativeSource</span></span><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small"> Self</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">},</span></span><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small"> Path</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">=(Validation.Errors).</span></span><span style="color:#ff0000;font-size:x-small"><span style="color:#ff0000;font-size:x-small">CurrentItem</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">.ErrorContent}&quot;&gt;</span></span> <br/><br/>This seems to have resolved the problem - can anyone comment as to whether this is a recommended solution to the problem <br/>Thanks,<br/>TimWed, 29 Apr 2009 00:43:55 Z2009-04-29T00:48:14Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#0be03aaa-9d6e-4668-9164-d27b35bdcc3chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1c6cd214-abce-4a8b-a919-0726dd81461a#0be03aaa-9d6e-4668-9164-d27b35bdcc3cSam Bent - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Sam%20Bent%20-%20MSFTCustom ValidationRule throws ArgumentOutOfRangeException<p>Using CurrentItem is certainly more robust than using a fixed index.   BTW, WPF 4.0 will have fixes for this issue.</p><hr class="sig">Dev Lead, Windows Presentation Foundation, WinFXMon, 06 Jul 2009 17:07:04 Z2009-07-06T17:07:04Z