C# WPF文本框验证客户端
我做了文本框验证 但我想在各个阶段的文本框上制作一些样式 当我的页面第一次加载时,文本框看起来像下面的样式。 当用户开始输入值时,如果值错误,则整个文本框背景将为红色。 当用户输入右值时,文本框的绿色边框为2PX。C# WPF文本框验证客户端,c#,wpf,textbox,wpf-controls,C#,Wpf,Textbox,Wpf Controls,我做了文本框验证 但我想在各个阶段的文本框上制作一些样式 当我的页面第一次加载时,文本框看起来像下面的样式。 当用户开始输入值时,如果值错误,则整个文本框背景将为红色。 当用户输入右值时,文本框的绿色边框为2PX。 我使用以下风格: <Style x:Key="TxtEmailStyle" TargetType="{x:Type TextBox}"> <Setter Property="Background" Value="#2d2f34">&l
我使用以下风格:
<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>
!!!!
下面是我的文本框
<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>
一个棘手/难看的方法是使用故事板(可能很棘手,因为您不想要动画,但您可以将动画时间更改为无限长),下面是代码片段:
<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>
在某个地方,例如,在文本中更改了事件处理程序
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!
}
Storyboard sb=this.FindResource(“ChangeBkColor”)作为故事板;
if(sb!=null&&SomeCondition1)
{
故事板.SetTarget(sb,this.txtPlayerID);
sb.Begin();//效果来了!
}
您应该为您的案例创建两个故事板,并在文本更改处理程序中,说明什么条件,并有选择地播放故事板。您做了还是想做的问题是什么?@Shrivallabh:我想根据用户输入更改文本框边框样式:-如果输入正确,则将其变为绿色,如果输入错误,背景为红色,如果没有输入,则默认文本框样式您可以显示您的
依赖属性
,该属性具有playerID
?我不确定是否理解该问题。假设您的viewmodel已正确绑定并正确验证了与文本框关联的属性,那么它应该可以正常工作。你的风格有点乱,如果是我的话,我会把它们全部去掉