动画不会在触发器退出时停止

时间:2014-03-10 07:07:46

标签: wpf

我尝试在触发器更改时更改动画 - 但旧动画不会停止:

<TabControl>
    <TabItem Header="Dummy"></TabItem>
    <TabItem>
        <TabItem.Header>
            <Path Margin="20,0" Width="40.3295" Height="32.775" Stretch="Fill"  Data="M 300 100 L 500 400 100 400 Z" Fill="Gold">                    
                <Path.Style>
                    <Style TargetType="Path">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="True">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)">
                                            <ColorAnimation RepeatBehavior="Forever" From="Green" To="Blue" Duration="0:0:1.5" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard FillBehavior="Stop" Duration="0" />
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="False">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)">
                                            <ColorAnimation RepeatBehavior="Forever" From="Red" To="Yellow" Duration="0:0:1.5" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard FillBehavior="Stop" Duration="0" />
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Path.Style>
            </Path>
        </TabItem.Header>
        <TextBlock>4</TextBlock>
    </TabItem>
</TabControl>

未选中Tab的动画一直在运行。有谁知道如何阻止它?谢谢:))

2 个答案:

答案 0 :(得分:4)

要停止使用BeginStoryBoard,因为名称表示它调用方法Begin to Storyboard。在停止使用StopStoryBoard。

 <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Name="start">
                                            <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)">
                                                <ColorAnimation RepeatBehavior="Forever" From="Green" To="Blue" Duration="0:0:1.5" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="start"/>
                                    </DataTrigger.ExitActions>
                                </DataTrigger>

答案 1 :(得分:0)

试试这个

  <TabControl>
        <TabItem Header="Dummy"></TabItem>
        <TabItem>
            <TabItem.Header>
                <Path Margin="20,0" Width="40.3295" Height="32.775" Stretch="Fill"  Data="M 300 100 L 500 400 100 400 Z" Fill="Gold">
                    <Path.Style>
                        <Style TargetType="Path">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard x:Name="start">
                                            <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)">
                                                <ColorAnimation RepeatBehavior="Forever" From="Green" To="Blue" Duration="0:0:1.5" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <BeginStoryboard>
                                            <Storyboard FillBehavior="Stop" Duration="0" />
                                        </BeginStoryboard>
                                    </DataTrigger.ExitActions>                                      
                                </DataTrigger>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=IsSelected}" Value="False">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Name="UnselectedStoryboard">
                                            <Storyboard Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)">
                                                <ColorAnimation RepeatBehavior="Forever" From="Red" To="Yellow" Duration="0:0:1.5" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="UnselectedStoryboard" ></StopStoryboard>
                                    </DataTrigger.ExitActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Path.Style>
                </Path>
            </TabItem.Header>
            <TextBlock>4</TextBlock>
        </TabItem>
    </TabControl>