【问题标题】:WPF -- Animation QuestionWPF——动画问题
【发布时间】:2010-11-17 08:26:15
【问题描述】:

WPF 新手。简单的场景。无法找出正确的方法来做到这一点。

假设我有一个按钮。我也有四个文本块。我希望一个按钮同时触发所有 TextBlocks 上的动画(不透明度从 0 到 1)。

提前致谢!

【问题讨论】:

    标签: wpf animation triggers storyboard


    【解决方案1】:

    应该这样做...

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.MainWindow"
        x:Name="Window"
        Title="MainWindow"
        Width="640" Height="480">
        <Window.Resources>
            <Storyboard x:Key="OnClick1">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBlock" Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBlock1" Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBlock2" Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBlock3" Storyboard.TargetProperty="(UIElement.Opacity)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                    <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </Window.Resources>
        <Window.Triggers>
            <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
                <BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
            </EventTrigger>
        </Window.Triggers>
    
        <Grid x:Name="LayoutRoot">
            <StackPanel>
                <Button x:Name="button" Width="131" Height="37" Content="Button" Margin="0,0,0,22"/>
                <TextBlock x:Name="textBlock" Height="27" Text="TextBlock 1" TextWrapping="Wrap" Opacity="0"/>
                <TextBlock x:Name="textBlock1" Height="27" Text="TextBlock 2" TextWrapping="Wrap" Opacity="0"/>
                <TextBlock x:Name="textBlock2" Height="27" Text="TextBlock 3" TextWrapping="Wrap" Opacity="0"/>
                <TextBlock x:Name="textBlock3" Height="27" Text="TextBlock 4" TextWrapping="Wrap" Opacity="0"/>
            </StackPanel>
        </Grid>
    </Window>
    

    【讨论】:

    • 没有更好的办法吗?我对动画不是很有经验,但是重复同样的事情 4 次似乎是错误的......
    • 谢谢,谢谢,谢谢...现在,只是为了更深入一点,如果我有一个 X 文本块块,比如说在一个网格列中。我怎样才能将情节提要分配给该网格中的所有文本块,而不必分别为每个文本块做动画?
    • 如果您要为一堆元素制作大量类似的动画,您可能希望在代码中定义它们并以编程方式为情节提要生成动画。 AFAIK,没有纯 Xaml 方法可以做到这一点。
    • 有一种纯 xaml 方法可以做到这一点。 stackoverflow.com/questions/1238515/…
    • 不错的把戏。我在stackoverflow.com/questions/1238515/… 发布了您的解决方案的一个轻微变体,它适用于多个触发器,而无需向按钮添加 DataContext。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2012-05-29
    • 2012-05-03
    • 2014-08-02
    相关资源
    最近更新 更多