默认ErrorTemplate与自定义ErrorTemplate一起显示

本文关键字:ErrorTemplate 显示 一起 默认 自定义 | 更新日期: 2023-09-27 18:24:09

我有一个自定义的Validation.ErrorTemplate,由于某种原因,WPF同时显示了我的自定义错误模板和默认错误模板。它们都显示了与预期相同的错误,但是我不想显示默认的ErrorTemplate。

我的代码:

<Style TargetType="TextBox" x:Key="MyTextBox">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Border BorderBrush="red" BorderThickness="1" Background="#11FF0000" IsHitTestVisible="False" x:Name="errorBorder"/>
                    <AdornedElementPlaceholder x:Name="placeholder" />
                    <Popup AllowsTransparency="True" HorizontalAlignment="Right" HorizontalOffset="0" VerticalOffset="0" PopupAnimation="Fade" 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,4" Fill="red" Stretch="Fill" Stroke="red"
                                  StrokeThickness="2" />
                            <Border Background="red" CornerRadius="0" Padding="4">
                                <TextBlock HorizontalAlignment="Center" Foreground="white" FontWeight="Bold" Margin="2,0,0,0"
                                               Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" />
                            </Border>
                        </StackPanel>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

这个

<Style TargetType="TextBox" BasedOn="{StaticResource MyTextBox}"/>

我想知道是否有人知道为什么WPF同时显示我的错误模板和默认模板。

编辑http://i58.tinypic.com/a14k6q.png-两个错误都显示的图片

默认ErrorTemplate与自定义ErrorTemplate一起显示

因为您定义了ErrorTemplate,还定义了样式中的Validation.HasError触发器,所以可以使用其中一个触发器。如果你想使用ErrorTemplate,你需要删除触发器,并将Text绑定更改为"Path=AdornedElement.Validation.Errors).CurrentItem.ErrorContent",那么你只会看到ErrorTemplate:的结果

<Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Border/>
                    <AdornedElementPlaceholder x:Name="placeholder" />
                    <Popup>
                        <StackPanel>
                            <Polygon/>
                            <Border>
             <TextBlock Text="{Binding ElementName=placeholder, 
                        Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent,
                        Mode=OneWay}" />
                            </Border>
                        </StackPanel>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>