Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Custom ValidationRule throws ArgumentOutOfRangeException

$
0
0
Hi  there.

I've been developing a custom ValidationRule, following the MS example by the letter but it is throwing an ArhumentOutOfRangeException during the validation process.

I have the following ValidationRule

Code Snippet

public class MyValidationRule : ValidationRule
{

public override System.Windows.Controls.ValidationResult Validate(object value, CultureInfo cultureInfo)
{
...

          if ( errorCondition1 )
return new ValidationResult(false);

if ( errorCondition2)
return new ValidationResult(false,"Error 2");

return ValidationResult.ValidResult;
}

}



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

Code Snippet
<local:MyTextBox x:Name="_myTextBox" Validation.ErrorTemplate="{StaticResource TextBoxValidationTemplate}" Style="{StaticResource TextBoxInError}">
<local:MyTextBox.Text>
<Binding Path="MyText" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:MyValidationRule/>
</Binding.ValidationRules>
</Binding>
</local:MyTextBox.Text>
</local:MyTextBox>

The TextBoxInError style is defined as follows

Code Snippet
<Style x:Key="TextBoxInError" TargetType="{x:Type local:MyTextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},                                       Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>


Validation is performed as expected, and the tooltip is correctly set. However, when the ValidationResult changes, the following error is reported


Code Snippet

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. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
   at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'

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.



Any pointers appreciated.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>