【问题标题】:WPF ErrorTemplate visible when not focused?WPF ErrorTemplate 不集中时可见?
【发布时间】:2009-07-28 13:43:27
【问题描述】:

我正在使用 WPF 验证进行 TextBox 验证。我已经定义了这个模板:

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}" BasedOn="{StaticResource StyleTextBox}">        
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                <Setter Property="Background" Value="{StaticResource TextBox_ErrorBackgroundBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource TextBox_ErrorBorderBrush}"/>
                <Setter Property="BorderThickness" Value="2"/>                      
            </Trigger>
        </Style.Triggers>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="20" Text="!"/>
                        <AdornedElementPlaceholder/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  

TextBox 位于 TabItem 中的表单上。 一切正常,但“!”当我选择其他 TabItems 时,TextBlock 保持可见。在许多其他情况下会观察到这种行为 - 当扩展器展开等时。虽然没有显示 TextBox,但 Excklamation 始终保持在同一位置可见。

【问题讨论】:

    标签: wpf validation templates focus


    【解决方案1】:

    这就是我们所做的......

    <Style x:Key="ErrorTemplate" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <!--Set your error template in here-->
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsVisible" Value="false">
                <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 这有一个问题!在某些情况下,WPF 通过呈现抛出异常(无法将值 NULL 添加到集合中)。当我在 UserControl 中有 TextBox 并将其保存在缓存中并从缓存中重新加载实例时,就会发生这种情况。
    猜你喜欢
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多