1

I have looked at many problems and solutions regarding borders not moving with elements but none of the solutions have worked for me.

Here is a screenshot:

problematic validation

I have a basic template for validation:

    <ControlTemplate x:Key="ErrorTemplate">
        <Grid Loaded="ErrorTemplateLoaded">
            <Border x:Name="errorBorder" DockPanel.Dock="Top" BorderBrush="Orange" BorderThickness="1"/>
            <AdornedElementPlaceholder x:Name="placeholder"/>
            <Popup AllowsTransparency="True" Placement="Right"
                       PlacementTarget="{Binding ElementName=errorBorder}"
                       IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}">
                <StackPanel Orientation="Horizontal">
                    <Polygon  VerticalAlignment="Center" Points="0,4 4,0 4,8" Fill="#FFE4B3" Stretch="Fill" Stroke="#FFE4B3"
                                  StrokeThickness="2" />
                    <Border Background="#FFE4B3" CornerRadius="4" Padding="4">
                        <TextBlock HorizontalAlignment="Center" Foreground="Black" FontWeight="Bold" Margin="2,0,0,0"
                                   Text="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent, Mode=OneWay}" />
                    </Border>
                </StackPanel>
            </Popup>
        </Grid>
    </ControlTemplate>

Through Styles this gets applied to every element I need it applied to:

    <Style TargetType="CheckBox" BasedOn="{StaticResource DisableWhileLoadingProjectStyle}">
        <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}" />
    </Style>

This means it also gets applied to Checkboxes inside my TreeView which is nice and good but when I scroll within the TreeView that's where chaos ensues.

The Border does not seem to get updated after the scroll. Only after the next scroll the border is updated with the last position of the checkbox. This means that it is not synched with the current position.

I have tried finding the ScrollViewer in code-behind and used the ScrollChanged event to updated the border position but that did not help. My guess is that all events get the before scroll position of the element and not after scroll.

How can I solve this problem?

RedX
  • 14,749
  • 1
  • 53
  • 76
  • I can't see your `TreeViewItem` Template XAML anywhere but have you had a look at [this SO post](https://stackoverflow.com/a/10295861/2029607). It claims that you need to wrap the `TreeViewItem` in an `AdornerDecorator`. But again you might be doing that already. – XAMlMAX Aug 02 '17 at 13:50
  • @XAMlMAX I don't have a `TreeViewItem` template because I use various `DataTemplates` and `HierarchicalDataTemplate` to build the `TreeView`. I tried wrapping the checkboxes in `AdornedDecorator` and it got better when opening the leafs but the problem still happens when scrolling. – RedX Aug 02 '17 at 14:23
  • 1
    @XAMlMAX If you add your comment as an answer I'll accept it. That worked. I had to wrap every `DataTemplate` used in an `AdornedDecorator`. – RedX Aug 02 '17 at 14:38

1 Answers1

0

What you are facing here is similar issue to this post on SO.
Solution to the problem was to wrap the CheckBox in an AdornerDecorator element, which had solved the problem.
I wish I could explain why this happens, all I can think of is that the AdorenerDecorator has additional logic to handle ErrorTemplates attached to it.

XAMlMAX
  • 2,268
  • 1
  • 14
  • 23