如果在 Window.Resources 中添加,WPF 装饰器将无法工作

WPF Adorner Not Working if Added in Window.Resources

我正在使用 .NET Framework 4.5,但遇到以下问题。

如果我像这样添加 Validation ErrorTemplate,我的装饰器将正常工作并在我的 TextBox 旁边显示工具提示和红色圆圈:

// THIS IS WORKING FINE BUT ONLY FOR txtAge TextBox
<TextBox x:Name="txtAge" 
         Validation.Error="Validation_Error"
         Text="{Binding UpdateSourceTrigger=LostFocus, Path=Age, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
         HorizontalAlignment="Left" MaxLength="3" Width="50">

    <Validation.ErrorTemplate>
        <ControlTemplate>
            <DockPanel LastChildFill="true">
                <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                    <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/>
                </Border>
                <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                    <Border BorderBrush="red" BorderThickness="1" />
                </AdornedElementPlaceholder>
            </DockPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>
</TextBox>

因此,上面的验证模板已添加到我的文本框 txtAge 的 <TextBox> 标记中,因此仅适用于该文本框。

但是,我想要一个适用于所有文本框的样式,所以我在 <Window.Resources> 标签内添加了装饰器。但这既不会显示工具提示,也不会显示红色圆圈:

// I WANT TO MAKE IT APPLY TO ALL TEXTBOXES BUT THIS IS NOT WORKING
<Window.Resources>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Margin" Value="5,0,5,0" />
        <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="0,2,40,2" />
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="true">
                        <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                            <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/>
                        </Border>
                        <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                            <Border BorderBrush="red" BorderThickness="1" />
                        </AdornedElementPlaceholder>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

为什么第一个有效而第二个无效?我是 WPF 新手。

根据@SnowballTwo 的回答,我明白了。

将代码移至 Windows.Resources 部分并添加 x:Key 如下所示:

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

然后为每个 TextBox 添加以下行以引用 ControlTemplate

Validation.ErrorTemplate="{StaticResource ValidationTemplate}"