WPF错误模板可见时不聚焦? [英] WPF ErrorTemplate visible when not focused?

查看:142
本文介绍了WPF错误模板可见时不聚焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < Style x:Key =textBoxInErrorTargetType ={x:Type TextBox} BasedOn ={StaticResource StyleTextBox}> 
< Style.Triggers>
< Trigger Property =Validation.HasErrorValue =true>
< Setter Property =ToolTip
Value ={Binding RelativeSource = {x:Static RelativeSource.Self},
Path =(Validation.Errors)[0] .ErrorContent} />
< Setter Property =BackgroundValue ={StaticResource TextBox_ErrorBackgroundBrush}/>
< Setter Property =BorderBrushValue ={StaticResource TextBox_ErrorBorderBrush}/>
< Setter Property =BorderThicknessValue =2/>
< / Trigger>
< Setter Property =Validation.ErrorTemplate>
< Setter.Value>
< ControlTemplate>
< DockPanel>
< TextBlock DockPanel.Dock =RightForeground =RedFontSize =20Text =!/>
< AdornedElementPlaceholder />
< / DockPanel>
< / ControlTemplate>
< / Setter>
< / style>

TextBox位于TabItem的窗体中。
一切工作正常,但是当我选择其他TabItem时,'!'TextBlock保持可见。在许多其他情况下,这种行为是被观察到的 - 当扩展器展开等等时,Excklamation总是保持在同一个地方,尽管TextBox没有被显示出来。

解决方案

 < Style x:Key =ErrorTemplateTargetType = 控制 > 
< Setter Property =Validation.ErrorTemplate>
< Setter.Value>
<! - 在这里设置你的错误模板 - >
< / Setter>
< Style.Triggers>
<触发属性=IsVisible值=假>
< Setter Property =Validation.ErrorTemplateValue ={x:Null}/>
< / Trigger>
< / style>


I'm using a WPF validation for TextBox validation. I have defined this Template:

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

The TextBox is located on a form in a TabItem. Everything works fine, but the '!' TextBlock stays visible when I choose other TabItems. This behaviour is observed in many other cases - when expander expands etc.. the Excklamation always stays visible on the same place, although the TextBox is not diplayed.

解决方案

This is what we do...

<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错误模板可见时不聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆