我制作了自己的默认TextBlock样式,但遇到了问题。
有一些触发器-GotFocus / LostFocus事件触发器和附加的属性触发器。
触发第一个事件触发器时,边框颜色将变为绿色,然后变为灰色,而附加的属性触发器会将其颜色更改为红色。
有一个问题:如果我单击按钮以运行验证TextBoxes的方法,它们将切换为红色,但是如果我先单击任何TextBox来打开GotFocus / LostFocus触发器,然后尝试验证它们是否单击了不会将其颜色更改为红色。
希望你能理解我
代码如下:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontFamily" Value="{StaticResource LatoRegular}"/>
<Setter Property="BorderBrush" Value="{StaticResource DarkGreyBrush}" />
<Setter Property="BorderThickness" Value="0 0 0 2" />
<Setter Property="Margin" Value="0 0 0 0"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="local:FormMessageProperty.Value" Value="None"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid Margin="{TemplateBinding Margin}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" x:Name="Border" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Margin="0 20 0 0" Padding="5 1 5 1">
<ScrollViewer x:Name="PART_ContentHost" Focusable="True"
HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<TextBlock Grid.Row="0" x:Name="PlaceholderText" Text="{TemplateBinding Tag}" Foreground="{StaticResource DarkGreyBrush}"
FontFamily="{StaticResource LatoLight}" TextAlignment="{TemplateBinding TextAlignment}" FontSize="{TemplateBinding FontSize}"
IsHitTestVisible="False" Margin="0 22 0 0" Padding="5 0 5 0"/>
<TextBlock Grid.Row="1" Text="{TemplateBinding local:FormMessageProperty.Value, Converter={local:FormMessageToStringConverter}}"
Foreground="Red" FontFamily="{StaticResource LatoRegular}" FontSize="12" Margin="5 3 5 0"
x:Name="ErrorMessage"/>
</Grid>
<!-- Animations -->
<ControlTemplate.Triggers>
<!-- GotFocus animations -->
<EventTrigger RoutedEvent="GotFocus">
<!-- Border -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="{StaticResource ButtonGreen}" Duration="0:0:0.2"
Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush.Color" />
</Storyboard>
</BeginStoryboard>
<!-- Text -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="{StaticResource ButtonGreen}" Duration="0:0:0.2"
Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- LostFocus animations -->
<EventTrigger RoutedEvent="LostFocus">
<!-- Border -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="{StaticResource DarkGrey}" Duration="0:0:0.2"
Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush.Color" />
</Storyboard>
</BeginStoryboard>
<!-- Text -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="{StaticResource DarkGrey}" Duration="0:0:0.2"
Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- Placeholder Text Animations -->
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}, Converter={local:TextToBooleanConverter}, UpdateSourceTrigger=PropertyChanged}" Value="True">
<!-- Go up -->
<DataTrigger.EnterActions>
<!-- Margin -->
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="Margin"
To="0 4 0 0" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
<!-- FontSize -->
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="FontSize"
To="14" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<!-- Go down -->
<DataTrigger.ExitActions>
<!-- Margin -->
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="Margin"
To="0 22 0 0" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
<!-- FontSize -->
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="FontSize"
To="18" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
<!-- FormMessageProperty animations -->
<Trigger Property="local:FormMessageProperty.Value" Value="None">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ErrorMessage" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<!-- When FormMessageProperty is other than None -->
<Trigger.ExitActions>
<!-- Error Message -->
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ErrorMessage" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
<!-- Border -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Red" Duration="0:0:0.2"
Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush.Color" />
</Storyboard>
</BeginStoryboard>
<!-- Text -->
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Red" Duration="0:0:0.2"
Storyboard.TargetName="PlaceholderText" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>