简体   繁体   English

WPF文本框验证客户端

[英]WPF textbox validation client side

I am did text box validation. 我做了文本框验证。

But i want to make some style on text box on various phase. 但我想在各个阶段的文本框上做一些风格。

When my page 1st time load the textbox looks like following style. 当我的页面第一次加载文本框时看起来像下面的样式。 在此输入图像描述

When User start entering value and if value is wrong the entire textbox background will be RED. 当用户开始输入值并且值错误时,整个文本框背景将为红色。 在此输入图像描述

When user enter right value the Text box have GREEN Border with 2PX. 当用户输入正确的值时,文本框具有带2PX的绿色边框。
在此输入图像描述

I am Using Following Style : 我正在使用以下风格:

  <Style x:Key="TxtEmailStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="#2d2f34"></Setter>
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="TextBlock.FontSize" Value="14" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="BorderBrush" Value="Green"></Setter>
        <Setter Property="BorderThickness" Value="2"></Setter>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" 
                        Foreground="Orange"
                        FontSize="12pt">
                        !!!!
                        </TextBlock>
                        <Border BorderBrush="Green" BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="Background" Value="#56585e" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="#07839a" />
            </Trigger>
            <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="#cb0b38"></Setter>
            </Trigger>
            <!--<Trigger Property="Validation.HasError" Value="false">
                <Setter Property="BorderBrush" Value="Green"></Setter>
                <Setter Property="BorderThickness" Value="2"></Setter>
                <Setter Property="Background" Value="#2d2f34"></Setter>
            </Trigger>-->

        </Style.Triggers>
    </Style>

The following is my TextBox 以下是我的TextBox

 <TextBox x:Name="txtPlayerID"  HorizontalAlignment="Left" Height="30" TextWrapping="Wrap" 
                                     VerticalContentAlignment="Center"
                    Style="{StaticResource TxtEmailStyle}" VerticalAlignment="Top" Width="228" FontFamily="Arial Regular"
                    RenderTransformOrigin="0.408,-2.455" FontSize="14"
                                     Margin="27,0,0,0">

                                <TextBox.Text>

                                    <Binding Path="playerID" Source="{StaticResource Register}"
                          ValidatesOnDataErrors="True"     NotifyOnValidationError="True"
                         UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>

                                            <ExceptionValidationRule/>
                                        </Binding.ValidationRules>
                                    </Binding>
                                </TextBox.Text>
                            </TextBox>

A tricky/ugly approach is to use a story board (might be tricky, as you don't want animation, but you can change the animation time to infinite anyway) below is the code snippet: 一个棘手/丑陋的方法是使用故事板(可能很棘手,因为你不想要动画,但你可以将动画时间改为无限)下面是代码片段:

<Storyboard x:Key="ChangeBkColor" Storyboard.TargetProperty="(TextBox.Background)">
    <ColorAnimation Storyboard.TargetProperty="Background.Color" 
             From="Red" To="Red" Duration="0:0:10"/> <!--Change 10 secs to yours-->
</Storyboard>

And somewhere, eg in the text changed event handler 在某处,例如在文本中更改了事件处理程序

Storyboard sb = this.FindResource("ChangeBkColor") as Storyboard;
if (sb != null && SomeCondition1)  <!--SomeCondition1: for red background-->
{
     Storyboard.SetTarget(sb, this.txtPlayerID);
     sb.Begin();  // Here comes the effect!
}

You should create two story boards for your case, and in the text changed handler, tell what conditions, and selectively play the story boards. 您应该为您的案例创建两个故事板,并在文本中更改处理程序,告诉您什么条件,并有选择地播放故事板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM