You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF自定义Validation.ErrorTemplate不显示问题求助

问题原因及解决方案

你的核心问题是混淆了Validation.ErrorTemplate和ToolTip的模板用途,导致自定义错误提示无法正常渲染,具体错误点和修正方法如下:

错误点分析

  • Validation.ErrorTemplate的目标类型错误:你给它赋值了一个TargetType="ToolTip"的模板,但ErrorTemplate是用于渲染控件错误装饰层的模板,其上下文是ValidationError对象,而非ToolTip,WPF无法识别该模板作为错误提示渲染。
  • 缺失AdornedElementPlaceholder:ErrorTemplate必须包含这个元素,用来关联到触发错误的原控件,否则WPF不知道错误提示应该附着在哪个控件上。
  • 绑定路径错误:ErrorTemplate的DataContext是当前的ValidationError,不需要通过(Validation.Errors)[0]来获取错误内容,直接绑定ErrorContent即可。

修正后的代码

第一步:修正错误提示模板(ErrorTemplate)

将原来的ToolTip模板改成符合ErrorTemplate要求的结构:

<ControlTemplate x:Key="CommonValidationErrorTemplate" TargetType="{x:Type Control}">
    <Grid>
        <!-- 关联原控件 -->
        <AdornedElementPlaceholder x:Name="AdornedElement"/>
        <!-- 自定义错误提示框 -->
        <Popup IsOpen="{Binding ElementName=AdornedElement, Path=Validation.HasError}"
               Placement="Right"
               PlacementTarget="{Binding ElementName=AdornedElement}"
               AllowsTransparency="True">
            <Grid RenderTransformOrigin="0,0">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup Name="OpenStates">
                        <VisualStateGroup.Transitions>
                            <VisualTransition GeneratedDuration="0"/>
                            <VisualTransition To="Open" GeneratedDuration="0:0:0">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0"/>
                                </Storyboard>
                            </VisualTransition>
                        </VisualStateGroup.Transitions>
                        <VisualState x:Name="Closed">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Open">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>

                <Border x:Name="Root" Opacity="0" Margin="4,4,-4,-4" Background="#052A2E31" CornerRadius="5"/>
                <Border Margin="3,3,-3,-3" Background="#152A2E31" CornerRadius="4"/>
                <Border Margin="2,2,-2,-2" Background="#252A2E31" CornerRadius="3"/>
                <Border Margin="1,1,-1,-1" Background="#352A2E31" CornerRadius="2"/>

                <Border Background="#FFDC000C" CornerRadius="2">
                    <TextBlock Foreground="White" Margin="8,4,8,4" MaxWidth="250" TextWrapping="Wrap" 
                               Text="{Binding ErrorContent}"/>
                </Border>
            </Grid>
        </Popup>
    </Grid>
</ControlTemplate>

第二步:修正应用模板的样式

调整样式,确保ErrorTemplate使用上面修正后的模板,同时保留ToolTip的设置(如果需要):

<Style x:Key="ValidatingControlStyle" TargetType="{x:Type FrameworkElement}">
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource CommonValidationErrorTemplate}"/>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

额外说明

  • 如果你只需要鼠标悬停时显示自定义ToolTip,不需要控件旁的错误提示,可以去掉Validation.ErrorTemplate的设置,直接自定义ToolTip的模板,并在触发器中赋值给ToolTip属性。
  • 确保你的控件已经正确设置了验证规则(比如INotifyDataErrorInfo或ValidationRule),并且Validation.HasError确实为True。

内容的提问来源于stack exchange,提问作者Abid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 07:35:27