WPF:如何将简单的动画应用于标签?

发布于 2024-10-22 06:29:01 字数 1294 浏览 3 评论 0原文

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

但这不起作用:

<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>

我想看看标签如何正在屏幕上淡入。

提前致谢。

I have a label with an image inside. I'd like to apply a simple animation: changing the opacity property for achieving a fade-in effect after the label is loaded (or when is visible or whatever)

But this doesn't work:

<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>

I'd like to see how the label is fading in on the screen.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏闹i 2024-10-29 06:29:01

试试这个:

<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>

Try this:

<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>
给妤﹃绝世温柔 2024-10-29 06:29:01

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

If you remove the Visibility="Hidden", it will work (assuming you want it to fade in when it loads). Otherwise, set the Visibility to Visibility.Visible from code-behind when you want it to start fade in. Also, make sure that in the ContentTemplate you have something visible.

静谧幽蓝 2024-10-29 06:29:01

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

C# 示例:

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

Are you setting the label visible in your code somewhere? You have to set it to be visible in order for the animation to trigger.

C# example:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文