WPF:如何将简单动画应用于Label?

时间:2011-03-17 21:09:40

标签: wpf animation triggers label

我有一张里面有图片的标签。我想应用一个简单的动画:更改不透明度属性,以便在加载标签后(或何时可见或其他)实现淡入效果

但这不起作用:

<Label ContentTemplate="{DynamicResource ImageLabelDataTemplate}"  Canvas.Left="36" Canvas.Top="394" Height="116" Name="PreviousVirtualButton" Width="100" Visibility="Hidden">
                <Label.Style>
                    <Style TargetType="Label">
                        <Style.Triggers>
                            <Trigger Property="IsVisible" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard TargetProperty="Opacity">
                                            <DoubleAnimation  Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:5" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>                            
                        </Style.Triggers>
                        </Style>
                </Label.Style>
            </Label>

我想知道标签在屏幕上是如何淡入的。

提前致谢。

3 个答案:

答案 0 :(得分:4)

试试这个:

<Window x:Class="WpfApplicationUnleashed.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Width="200" MinWidth="200" MaxWidth="300" MaxHeight="120" MinHeight="120" Height="120">
    <Grid>
        <Label Canvas.Left="36" Content="HELLO" Canvas.Top="394" Height="116" Name="PreviousVirtualButton" Width="100">
            <Label.Style>
                <Style TargetType="Label">
                    <Style.Triggers>
                        <EventTrigger RoutedEvent="Label.Loaded">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard TargetProperty="Opacity">
                                        <DoubleAnimation  Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:5" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
    </Grid>
</Window>

答案 1 :(得分:1)

如果删除Visibility="Hidden",它将起作用(假设您希望它在加载时淡入)。否则,如果希望它开始淡入,请将Visibility设置为代码隐藏。另外,请确保在ContentTemplate中有可见的内容。

答案 2 :(得分:1)

您是否在代码中将标签设置为可见?您必须将其设置为可见才能触发动画。

C#示例:

    previousVirtualButton.Visibility = System.Windows.Visibility.Visible;