【问题标题】:Validation Error Templates For UserControlUserControl 的验证错误模板
【发布时间】:2011-10-18 14:32:52
【问题描述】:

我已经建立了一个用户控件。当发生验证错误时,我不喜欢在它周围显示红色边框。我的控件中有一个文本框。

如何覆盖验证错误样式以消除整个控件中的红色边框,只在我的用户控件内的文本框中显示红色背景?

谢谢!

【问题讨论】:

    标签: wpf


    【解决方案1】:

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

     <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>
    

    我必须对你的 DocPanel 做的所有事情例如,对于我来说,控件位于 DockPanel 内,然后我必须将其 Validation.Error 模板设置为空,这将删除边框。

    例如:

        <TextBox >
           <Validation.ErrorTemplate>
             <ControlTemplate>
             </ControlTemplate>
           </Validation.ErrorTemplate>
         </TextBox>
    

    【讨论】:

    • 这对我不起作用。该行为表明父窗口的验证模板覆盖了用户控件的文本框样式。当然,这导致我尝试在父窗口中为无效的用户控件输入创建样式,但是我还没有找到任何方法来修改通过用户控件提供的文本框背景。如果您能提供更多解释,那就太好了!
    • 是的,您必须在用户控件而不是文本框上看到红色边框。这应该是因为父母也应该像我们为文本框所做的那样设置他们的Validation.ErrorTemplate。假设例如用户控件的 Root_LayoutContainer 是网格然后设置网格的 Validtion.ErrorTemplate 你不应该在你的用户控件上看到这个了。
    • 谢谢尼维德!我一直在转身,但最终得到了这个工作。我从父级(在我的情况下,是 MainWindow.xaml 中的网格)删除了应用于用户控件的任何 Validation.HasError 样式,并在用户控件 xaml 文件中的用户控件文件中编写了文本框的样式(当您演示)。我明白这一点,但我错过的是我需要在 usercontrol .cs 代码中实现 IDataErrorInfo 并验证依赖属性。我曾希望在父对象 xaml 和视图模型中设置验证会触发控件的组件。
    • 我不知道 Validation.ErrorTemplate 是否真的可用。如果我在那里设置了背景,那么控件就不会 clicable,因为它可能位于顶部。我想触发是最好的方法
    【解决方案2】:

    关于用户控件的样式:

    <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>
    

    【讨论】:

    • 这不起作用。即使我在没有 DynamicResource 的情况下设置 BG 颜色,它也不会显示。就像 Validation.HasError 属性没有链接到 UserControl 的。
    • 您需要确保您期望错误来自的属性具有 ValidatesOnDataErrors="true" 和 ValdidatesOnExceptions="true"
    猜你喜欢
    • 2013-02-04
    • 2016-12-03
    • 2018-06-03
    • 2016-07-11
    • 1970-01-01
    • 2017-04-30
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多