另一个控件上的StartStoryboard

时间:2014-06-09 18:42:56

标签: c# wpf storyboard

我有这个xaml:

<Window x:Class="StoryboardTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="RenderTransformOrigin" Value="0.5 0.5"/>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform x:Name="rt" Angle="0"/>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="1" Content="Start">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard >
                            <DoubleAnimation From="0" To="100" Storyboard.TargetName="pbar" Storyboard.TargetProperty="Value" Duration="0:0:6"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
            </Button>
        <Button Grid.Row="1" Content="1" x:Name="btn1">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard x:Name="bs1">
                        <Storyboard>
                            <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Duration="0:0:1" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
            </Button>
        <Button Grid.Row="1" Grid.Column="1" Content="2" x:Name="btn2">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard x:Name="bs2">
                        <Storyboard>
                            <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Duration="0:0:1" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
            </Button>
        <Button Grid.Row="1" Grid.Column="2" Content="3" x:Name="btn3">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard x:Name="bs3">
                        <Storyboard>
                            <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Duration="0:0:1" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <ProgressBar x:Name="pbar" Maximum="100" Grid.Row="2" Grid.ColumnSpan="3"/>
    </Grid>
</Window>

如您所见,有4个按钮(开始按钮 - 主按钮,按钮1,2和3)和进度条。当我点击按钮1,2或3时,它们会稍微旋转。我的问题是如何在Start的按钮EventTrigger中触发旋转(由各个故事板描述)?是否有可能实现这一点,而不是在“开始”按钮中编写3个其他故事板,而只是为xaml中的按钮1,2,3调用已经附加的故事板?

1 个答案:

答案 0 :(得分:0)

<Window.Resources>
    <Storyboard x:Key="StoryboardName">
        <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Storyboard.TargetName="btn1" Duration="0:0:1" AutoReverse="True"/>
        <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Storyboard.TargetName="btn2" Duration="0:0:1" AutoReverse="True"/>
        <DoubleAnimation From="0" To="90" Storyboard.TargetProperty="RenderTransform.Angle" Storyboard.TargetName="btn3" Duration="0:0:1" AutoReverse="True"/>
    </Storyboard>
</Window.Resources>

 <Button Grid.Column="1" Content="Start">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard Storyboard="{StaticResource StoryboardName}"/>
            </EventTrigger>
        </Button.Triggers>
        </Button>

故事板中声明的Doubleanimations将同时运行。

 <Button>
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=start, Path=IsPressed}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                ...
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>