简体   繁体   English

TreeView复选框中的Validation.Template不遵循TreeViewItem

[英]Validation.Template in Checkbox in TreeView does not follow TreeViewItem

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: 这是屏幕截图:

有问题的验证

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: 通过Styles它可以应用于我需要应用于的每个元素:

    <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. 这意味着它也被应用到TreeView内的Checkboxes中,这很好,但是当我在TreeView中滚动时,就会出现混乱。

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. 我尝试在代码隐藏中找到ScrollViewer,并使用ScrollChanged事件更新了边框位置,但这没有帮助。 My guess is that all events get the before scroll position of the element and not after scroll. 我的猜测是所有事件都在元素的滚动位置之前而不是滚动之后。

How can I solve this problem? 我怎么解决这个问题?

What you are facing here is similar issue to this post on SO . 您在这里面临的问题与此SO上的帖子类似。
Solution to the problem was to wrap the CheckBox in an AdornerDecorator element, which had solved the problem. 解决该问题的方法是将CheckBox包装在一个AdornerDecorator元素中,该元素已解决了该问题。
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. 我希望我可以解释,为什么出现这种情况,所有我能想到的是,AdorenerDecorator有额外的逻辑来处理ErrorTemplates连接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM