使用动画制作 WPF 标签(或其他元素)闪光
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15822519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a WPF Label (or other element) flash using animation
提问by Dave
I have a label that I only make visible based on one of my ViewModel Properties. Here is the XAML:
我有一个标签,仅根据我的 ViewModel 属性之一使其可见。这是 XAML:
<Label HorizontalAlignment="Center" VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontSize="24" Width="200" Height="200" >
<Label.Content >
Option in the money!
</Label.Content>
<Label.Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding OptionInMoney}" Value="True">
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
I'm not sure this is the best way, but in any case, I'd also like to have the label flashing. Clearly, I only want it flashing when it is visible. Can someone point me to some example code, or write a quick example to do this? I assume I need some sort of trigger, and an animation. Presumably I also need a trigger when the label is no longer visible so that I stop the animation?
我不确定这是最好的方法,但无论如何,我也想让标签闪烁。显然,我只希望它在可见时闪烁。有人可以指出一些示例代码,或者编写一个快速示例来执行此操作吗?我想我需要某种触发器和动画。据推测,当标签不再可见时,我还需要一个触发器,以便停止动画?
Thanks, Dave P.S. Is there a good book or site for all these WPF tricks? Something like the "MFC Answer Book" for those that remember that book.
谢谢, Dave PS 有没有一本关于所有这些 WPF 技巧的好书或网站?对于那些记得那本书的人来说,类似于“MFC Answer Book”。
回答by sa_ddam213
You could add a Storyboard
animation to the Style.Resources
and start it in the EnterActions
section of the DataTrigger
.
您可以向 中添加Storyboard
动画Style.Resources
并EnterActions
在DataTrigger
.
A simple DoubleAnimation
on the Opacity
should work fine
一个简单DoubleAnimation
的Opacity
应该可以正常工作
Something like this:
像这样的东西:
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Resources>
<Storyboard x:Key="flashAnimation" >
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
</Storyboard>
</Style.Resources>
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding OptionInMoney}" Value="True">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger.EnterActions>
<BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimation}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="flash"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
回答by NishantM
StoryBoard is certainly the WPF way, but it can be achieved by a simple code also. Here it goes, to make a label background blink:
StoryBoard 当然是 WPF 的方式,但也可以通过简单的代码来实现。在这里,让标签背景闪烁:
lblTimeris a Lebel on your form with some text, say, "I AM BLINKING"
lblTimer是表单上的 Lebel,带有一些文字,比如“我正在眨眼”
This can be applied to any property, as VISIBILITY.
这可以应用于任何属性,如 VISIBILITY。
// Create a timer.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
timer.Start();
}
// The timer's Tick event.
private bool BlinkOn = false;
private void timer_Tick(object sender, EventArgs e)
{
if (BlinkOn)
{
lblTimer.Foreground = Brushes.Black;
lblTimer.Background = Brushes.White;
}
else
{
lblTimer.Foreground = Brushes.White;
lblTimer.Background = Brushes.Black;
}
BlinkOn = !BlinkOn;
}