为了表示错误,我想暂时更改按钮的背景颜色.我是WPF动画的新手,无法找到一个简单的例子来继续.更复杂的是,我正在使用Trigger来处理错误通知.
所以这是我的XAML,我想知道如何用动画替换背景设定器(比如,在五秒内闪烁红色三次或类似的东西).
<UserControl>
<UserControl.Resources>
<Style x:Key="ErrorStyle" TargetType="Button">
<!--Clear the default error template (a red border)-->
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
<!--TODO: Replace with animation-->
<Setter Property="Background" Value="Orange"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Button Command="{Binding ProgramCommand, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"
Style="{StaticResource ErrorStyle}">
_Program
</Button>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我也对更好(简单)错误通知的建议持开放态度.
你可以用Trigger.EnterActions它.
<Trigger Property="Validation.HasError" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:0.3"
From="White" To="Red" RepeatBehavior="3x" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
Run Code Online (Sandbox Code Playgroud)
(假设背景为a SolidColorBrush,因为Storyboard.TargetProperty它的Color属性点)
对于渐变,您也可以为特定光圈的颜色设置动画,例如
<TextBox Text="{Binding TestInt}">
<TextBox.Background>
<LinearGradientBrush>
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</TextBox.Background>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.GradientStops[0].Color" Duration="0:0:0.3"
From="White" To="Red" RepeatBehavior="3x" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5186 次 |
| 最近记录: |