Wpf UserControl的验证错误模板

Wpf UserControl的验证错误模板,wpf,Wpf,我已经建立了一个用户控件。我不喜欢在验证错误发生时在其周围显示红色边框。我控制着一个文本框 我如何重写验证错误样式以去掉整个控件中的红色边框,并在usercontrol内的文本框中显示红色背景 谢谢 我正在使用这个模板,它将为文本框的背景上色,而不是只显示边框 <UserControl.Resources> <Style TargetType="{x:Type TextBox}"> <Style.Triggers>

我已经建立了一个用户控件。我不喜欢在验证错误发生时在其周围显示红色边框。我控制着一个文本框

我如何重写验证错误样式以去掉整个控件中的红色边框,并在usercontrol内的文本框中显示红色背景


谢谢

我正在使用这个模板,它将为文本框的背景上色,而不是只显示边框

 <UserControl.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true" >
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="MistyRose"/>
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="BorderThickness" Value="1.0"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource 
                 Self},Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

关于用户控件的样式:

<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>

在文本框的样式上:

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border 
                      Name="Border"
                      CornerRadius="5" 
                      Padding="2"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      Background="{TemplateBinding Background}"
                      BorderThickness="{TemplateBinding BorderThickness}" >
                    <ScrollViewer Margin="0"  x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="LightGray"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                        <Setter Property="Foreground" Value="Gray"/>
                    </Trigger>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ErrorBorderColor}"/>
                        <Setter Property="Background" TargetName="Border" Value="{DynamicResource ErrorBackgroundColor}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>


这不起作用。即使我在没有DynamicSource的情况下设置BG颜色,它也不会显示。与Validation.HasError属性类似。HasError属性未链接到UserControl的。您需要确保预期错误来自的属性具有ValidateSonDaErrors=“true”和ValdidatesOnExceptions=“true”这对我不起作用。该行为表明父窗口的验证模板覆盖用户控件的文本框样式。当然,这导致我尝试在父窗口中为无效的用户控件输入创建样式,但我还没有找到任何方法来修改通过用户控件提供的文本框背景。如果你能提供更多的解释,那就太好了!是的,您必须在用户控件上看到红色边框,而不是文本框。这应该是因为家长也应该像我们为textbox所做的那样设置他们的
Validation.ErrorTemplate
。例如,假设用户控件的Root_LayoutContainer是grid,然后设置grid的Validtion.ErrorTemplate。您不应该在用户控件上再看到它。谢谢Nivid!我不停地转过身来,但最终还是成功了。我从父控件中删除了应用于用户控件的所有Validation.HasError样式(在我的示例中,是MainWindow.xaml中的网格),并将用户控件文件中文本框的样式写入到用户控件xaml文件中(如您所演示的)。我了解这一点,但我遗漏的是,我需要在usercontrol.cs代码中实现IDataErrorInfo并验证依赖项属性。我曾希望在父对象xaml和视图模型中设置验证会触发控件的组件。我不知道validation.ErrorTemplate是否真的可用。如果我在那里设置背景,那么控件就不可剪裁,因为它可能位于顶部。我想触发是最好的方法
<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border 
                      Name="Border"
                      CornerRadius="5" 
                      Padding="2"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      Background="{TemplateBinding Background}"
                      BorderThickness="{TemplateBinding BorderThickness}" >
                    <ScrollViewer Margin="0"  x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="LightGray"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                        <Setter Property="Foreground" Value="Gray"/>
                    </Trigger>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ErrorBorderColor}"/>
                        <Setter Property="Background" TargetName="Border" Value="{DynamicResource ErrorBackgroundColor}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>