开发者

Error Template Binding error wpf

I get this error in the console when I run my app:

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=AdornedElement.(0)[0].ErrorContent; DataItem='AdornedElementPlaceholder' (Name='placeholder'); target element is 'TextBlock' (Name=''); target property is 'ToolTip' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index'
A first chance exception of type 'NHibernate.Validator.Exceptions.ValidatorConfigurationException' occurred in NHibernate.Validator.dll

One of the things about this that I don't understand yet is that the application behavior is fine, as are the unit tests.

I am (obviously) using an NHibernate validator behind the scenes of an IDataErrorInfo implementation, but the bottom line is that both IDataErrorInfo.Error and IDataErrorInfo[columnName] will return a single string if there is an error, and null if there isn't.

I would guess the exception comes about because when there is no error, there is no index, but the binding for my error tooltip seems typical of examples on how to do it.

The binding for my error template is below.

Questions:

  1. shouldn't IDataErrorInfo[columnName] return null if no error is found?
  2. is my error template lacking? the validation.HasError part??
  3. is this even a problem?
  4. if it is a problem, and it is likely related to NHib validator, and suggestions?

text box style with error template:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="0,0,16,0" />        
    <!-- Error display -->
    <Setter Property="Validation.ErrorTemplate">
        <Setter.V开发者_StackOverflow中文版alue>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right" Text=" *" 
                               Foreground="Red" 
                               FontWeight="Bold" FontSize="16" 
                               ToolTip="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                    <Border BorderBrush="Red"  BorderThickness="1">
                        <AdornedElementPlaceholder Name="placeholder"></AdornedElementPlaceholder>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" Value="LightYellow"/>
        </Trigger>
    </Style.Triggers>
</Style>


  1. Based on the IDataErrorInfo documentation, it returns an empty string by default. So if there is no error, you should probably return string.Empty.

  2. From Josh Smith's blog, you can use CurrentItem, in place of the [0] to avoid the errors.


Purely from memory (so most likely wrong) I think that can also happen when you don't actually have an adorner layer above in the visual hierarchy; did you customize the Window templates by any chance? Obviously can't remember the exact exception but something similar happened to me a while back. Easiest way to rule this out is to place the adorner layer manually somewhere - you can look at the default Window control template on MSDN to see how it's used.

edit: actually that should result in a NRE so like I said, most likely wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜