c# - 自定义验证错误模板不尊重 ZIndex

标签 c# wpf validation xaml datatemplate

我有一个定义为 DataTemplate(对于“OrderEntryViewModel”)的 View ,其中包含 3 行网格内的菜单、ContentPresenter 和 Expander。 ContentPresenter 的内容绑定(bind)到另一个 viewModel“OrderViewModel”(其中有另一个 DataTemplate 定义的 View )。扩展器的 ZIndex 为 99,因此当它向上扩展时,它会扩展到任何其他控件(即 ContentPresenter)。

这一切都按预期工作,除非 ContentPresenter 的内容(OrderViewModel)有数据错误...我的 OrderView 在包含无效数据的控件周围显示自定义验证错误模板。发生的情况是,当我展开扩展器时,ContentPresenter 内的所有控件都被覆盖,但我显示的红色边框和感叹号仍然通过扩展器可见!我已验证我的扩展器的 ZIndex 为 99,ContentPresenter 的 ZIndex 为 0。有人可以帮助我吗?

这里有一些图像可以帮助解释:

First Image显示 View 未展开时的样子。

Second Image显示展开时 View 的样子。

我像这样定义验证错误模板:

<ControlTemplate x:Key="ValidationErrorTemplate">
    <DockPanel LastChildFill="true">
        <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="10" Height="10" CornerRadius="5" 
                ToolTip="{Binding AdornedElement.(Validation.Errors).CurrentItem.ErrorContent, ElementName=customAdorner}">
            <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="White"/>
        </Border>
        <AdornedElementPlaceholder x:Name="customAdorner" VerticalAlignment="Center">
            <Border BorderBrush="red" BorderThickness="1" />
        </AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>

并将其分配给像这样的特定控件(这就是我为 TextBox 执行此操作的方式):

<Style TargetType="{x:Type TextBox}" x:Key="ValidatedStyleTextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsLocked}" Value="True">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsLocked}" Value="False">
            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTemplate}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

最佳答案

This solution为我工作...只是在与我的扩展器相同的级别添加了一个 AdornerDecorator,所以现在,AdornerDecorator 内部的控件使用该层来显示,而不是窗口中的顶层层

关于c# - 自定义验证错误模板不尊重 ZIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11477518/

相关文章:

c# - 无法在我的 View 中绑定(bind)超链接命令和 ICommand

asp.net-mvc - .NET MVC 自定义验证(无数据注释)

asp.net-mvc - 在哪里以及如何验证和映射 ViewModel?

c# - SQL更新语句添加一个值

c# - ServiceStack 拦截请求/响应以进行验证

wpf - 在 Visual Studio 2012(RTM,在 Windows 8 RTM 上)中没有带有断点的 XAML 绑定(bind)调试?

java - spring mvc 表单验证失败后无法显示错误消息

c# - 查看 Linq-to-Entities 生成的实体 SQL

带有 post 的 C# HTTPS 请求

wpf - 在运行时删除图像