C# 从以前的状态返回时WPF DataTrigger重新启动动画
我正在处理WPF应用程序。我有一个ToggleButton,当它被选中时应该是绿色的,当按钮关联的模型中出现错误时,它会闪烁红色的光晕。(它是一个ObservableCollection Items控件)。当DataTrigger执行时,动画工作正常,但是如果我选择按钮,然后取消选择,我希望动画继续。换句话说,如果有一个错误与按钮模型(它是闪烁的红色),我选择它(现在它是绿色),然后我取消选择它,那么它应该返回到闪烁的红色。实际情况是,一旦取消选择按钮,它将保持红色状态而不闪烁。这是我的xamlC# 从以前的状态返回时WPF DataTrigger重新启动动画,c#,wpf,C#,Wpf,我正在处理WPF应用程序。我有一个ToggleButton,当它被选中时应该是绿色的,当按钮关联的模型中出现错误时,它会闪烁红色的光晕。(它是一个ObservableCollection Items控件)。当DataTrigger执行时,动画工作正常,但是如果我选择按钮,然后取消选择,我希望动画继续。换句话说,如果有一个错误与按钮模型(它是闪烁的红色),我选择它(现在它是绿色),然后我取消选择它,那么它应该返回到闪烁的红色。实际情况是,一旦取消选择按钮,它将保持红色状态而不闪烁。这是我的xaml
<!-- This template is for the selection buttons on the left hand side of the application. -->
<ControlTemplate x:Key="SelectionControlTemplate" TargetType="{x:Type ToggleButton}">
<Grid Width="Auto" Height="Auto" Margin="3" >
<Rectangle x:Name="BaseLayer" HorizontalAlignment="Left" Height="50" RadiusY="8" RadiusX="8" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="50" Fill="{DynamicResource FutureBlueButtonGradient}"/>
<Rectangle x:Name="GlowLayer" HorizontalAlignment="Left" Height="52.25" Width="52.25" RadiusY="6.5" RadiusX="6.5" Stroke="{x:Null}" StrokeThickness="1" VerticalAlignment="Top" Margin="-1.125"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<!-- Animation for blinking. -->
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="GlowLayer" RepeatBehavior="Forever">
<EasingColorKeyFrame KeyTime="0" Value="#FFFD0002"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="#3FFD0002"/>
<EasingColorKeyFrame KeyTime="0:0:1" Value="#FFFD0002"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<!-- Style Triggers -->
<ControlTemplate.Triggers>
<!-- ERROR CONDITION TRIGGER: Flashes a selection button glowing red when HasTripOrError property is set to true. -->
<DataTrigger Binding="{Binding HasTripOrError, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Stroke" TargetName="GlowLayer" Value="{StaticResource SolidRedGlowBrush}"/>
<Setter Property="Effect" TargetName="GlowLayer">
<Setter.Value>
<BlurEffect/>
</Setter.Value>
</Setter>
<Setter Property="Fill" TargetName="GlowLayer" Value="{StaticResource RadialGradientRedGlowBrush}"/>
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="ErrorGlowStoryBoard" Storyboard="{StaticResource Storyboard1}"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="ErrorGlowStoryBoard"/>
</DataTrigger.ExitActions>
</DataTrigger>
<!-- MOUSE OVER TRIGGER: Puts a white outline around the button and increases its brightness a little on mouse over. -->
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="GlowLayer" Value="#FFFDFFFE"/>
<Setter Property="Fill" TargetName="BaseLayer" Value="{StaticResource FutureBlueButtonMouseOverGradient}"/>
</Trigger>
<!-- SELECTED TRIGGER: Makes the selected control glow green. -->
<Trigger Property="IsChecked" Value="True">
<Setter Property="Effect" TargetName="GlowLayer">
<Setter.Value>
<BlurEffect/>
</Setter.Value>
</Setter>
<Setter Property="Fill" TargetName="GlowLayer" Value="{StaticResource RadialGradientGreenGlowBrush}"/>
<Setter Property="Stroke" TargetName="GlowLayer" Value="{StaticResource SolidGreenGlowBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
当IsSelected转换为false时,如何重新启动动画。将您的
IsSelected
属性编写为:
private bool _is_selected;
public bool IsSelected
{
get { return _is_selected; }
set
{
if (_is_selected != value)
{
_is_selected = value;
UpdateProperty("IsSelected");
//We also need to raise the HasTripOrError property here to resume
//The red glow animation if there is an error.
HasTripOrError = !HasTripOrError;
HasTripOrError = !HasTripOrError;
}
}
}
HasTripOrError
绑定到一个DataTrigger
。因此,它将仅在HasTripOrError
的值更改时执行
HasTripOrError = !HasTripOrError;
HasTripOrError = !HasTripOrError;
上述代码确保HasTripOrError
值将恢复为
最后是原始值,如果关联了任何绑定
,则
得到反映
将您的
IsSelected
属性写为:
private bool _is_selected;
public bool IsSelected
{
get { return _is_selected; }
set
{
if (_is_selected != value)
{
_is_selected = value;
UpdateProperty("IsSelected");
//We also need to raise the HasTripOrError property here to resume
//The red glow animation if there is an error.
HasTripOrError = !HasTripOrError;
HasTripOrError = !HasTripOrError;
}
}
}
HasTripOrError
绑定到一个DataTrigger
。因此,它将仅在HasTripOrError
的值更改时执行
HasTripOrError = !HasTripOrError;
HasTripOrError = !HasTripOrError;
上述代码确保HasTripOrError
值将恢复为
最后是原始值,如果关联了任何绑定
,则
得到反映
谢谢你的回复。这正是我们想要的。我忍不住觉得我没有按预期使用它。你认为我应该用什么来代替数据触发器吗?@ghost-well另一个解决方案取决于你的scnario。IsSelected和HasTripOrError之间有什么联系?您不能在DataTrigger中使用IsSelected吗?谢谢您的回复。这正是我们想要的。我忍不住觉得我没有按预期使用它。你认为我应该用什么来代替数据触发器吗?@ghost-well另一个解决方案取决于你的scnario。IsSelected和HasTripOrError之间有什么联系?你不能在DataTrigger中使用IsSelected吗?