使用动画制作WPF标签(或其他元素)

时间:2013-04-04 22:00:36

标签: wpf animation triggers datatrigger

我有一个标签,我只根据我的一个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>

我不确定这是最好的方法,但无论如何,我也希望标签闪烁。显然,我只希望它在可见时闪烁。有人能指点我一些示例代码,或写一个快速示例来做到这一点?我假设我需要某种触发器和动画。据推测,当标签不再可见时我还需要一个触发器,以便我停止动画?

谢谢, 戴夫 附:所有这些WPF技巧都有一本好书或网站吗?像那些记得那本书的人那样的“MFC答案书”。

3 个答案:

答案 0 :(得分:32)

您可以向Storyboard添加Style.Resources动画,然后在EnterActions的{​​{1}}部分启动它。

DataTrigger上的简单DoubleAnimation应该可以正常使用

这样的事情:

Opacity

答案 1 :(得分:2)

StoryBoard肯定是WPF方式,但它也可以通过一个简单的代码来实现。在这里,使标签背景闪烁:

lblTimer 是您表单上的Lebel,其中包含一些文字,例如“I I BLINKING”

这可以应用于任何属性,如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;
}

答案 2 :(得分:1)

试试this post。它被称为“闪烁文本块”&#39;但您可以轻松地将TextBox换成标签。