15

I've been using the following error adorner template for a long time now:

<ControlTemplate x:Key="ErrorAdornerTemplateStyle" TargetType="{x:Type Control}">
    <Grid ClipToBounds="False" >
        <Border BorderBrush="Red" BorderThickness="2" Margin="-1" 
         ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent }">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Polygon Points="15,15 15,0 0,0"
                 Fill="Red"
                 HorizontalAlignment="Right"
                 VerticalAlignment="Top" 
                 ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent }"/>
    </Grid>
</ControlTemplate>

... and it works at run-time just fine (as far as I can tell).

However, after a flurry of upgrades to VS and WPF and NET Standard 2 in the past month, I've noticed that the intellisense in my syles xaml file is giving me the following error for the CurrentItem identifier:

The property 'CurrentItem' was not found in type 'ReadOnlyObservableCollection'.

Is this just a nuisance VS bug or is VS alerting me to some kind of change in the WPF subsystem that I need to adapt to?

BCA
  • 7,776
  • 3
  • 38
  • 53
  • 2
    Does the result differ if you use the `/` for current item binding? Like `Path=AdornedElement.(Validation.Errors)/ErrorContent`. – grek40 Nov 29 '17 at 09:59
  • Wow, that actually solved it (both design-time and run-time work error-free). If you post an answer I will mark it as accepted – BCA Nov 29 '17 at 15:25

1 Answers1

44

The ReadOnlyObservableCollection itself doesn't expose a CurrentItem property. Instead, the CurrentItem is a concept of the CollectionView that is internally created when a collection of items is bound in WPF.

There is some special support to access the CurrentItem of a collection by using / in the binding path.

Change the binding path to Path=AdornedElement.(Validation.Errors)/ErrorContent to utilize this support.

grek40
  • 13,113
  • 1
  • 24
  • 50