使用数据触发器控制故事板,但只触发一次

时间:2011-09-14 01:57:36

标签: storyboard datatrigger

我使用数据触发器来控制一些故事板,但它只能触发一次。

 <Style x:Key="PropertyTriggerExampleButtonStyle" TargetType="{x:Type Button}">           
        <Setter Property="Width" Value="200" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding para}" Value="0">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Width"
              To="500" Duration="0:0:1" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>                 
            </DataTrigger>
            <DataTrigger Binding="{Binding para}" Value="1">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Width"
              To="200" Duration="0:0:1" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>                   
            </DataTrigger>            
        </Style.Triggers>
    </Style>
<Button Style="{StaticResource PropertyTriggerExampleButtonStyle}">
        button width will be changed
    </Button>

para是一个变量(已经实现了INotifyPropertyChanged接口),它将由另一个按钮控制。它的值是0或1。

但是当我单击按钮更改参数值时,故事板只会为每个值(0和1)触发一次。它永远不会在以后触发。

如果我将第二个故事板放入第一个数据触发器的ExitActions标记中。它会运作良好。但我有超过2个故事板需要控制......

以下代码运行良好,但我需要根据不同的值控制许多故事板(超过2个)....

<Style.Triggers>
            <DataTrigger Binding="{Binding para}" Value="0">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Width" To="500" Duration="0:0:1" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>     
                <DataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Width" To="200" Duration="0:0:1" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>                       
        </Style.Triggers>

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,然后我给了Timeline.FillBehavior="Stop",这对我有用。

检查: Microsoft - Timeline Class Reference - Timeline.FillBehavior Property

答案 1 :(得分:2)

您可以使用带有条件的System.Windows.Interaction.Interactivity来启动故事板。

的xmlns:ⅰ= “http://schemas.microsoft.com/expression/2010/interactivity”    的xmlns:EI = “http://schemas.microsoft.com/expression/2010/interactions”

<Border x:Name="brd" MinWidth="20" Height="20">
<TextBlock Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Border.Resources>
    <Storyboard x:Key="Story" Duration="1000"
            Storyboard.TargetName="brd"
            Storyboard.TargetProperty="Background">
        <ObjectAnimationUsingKeyFrames>
            <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="#81bb8c" />
            <DiscreteObjectKeyFrame KeyTime="0:0:0.9" Value="{x:Null}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="Story2" Duration="1000"
            Storyboard.TargetName="brd"
            Storyboard.TargetProperty="Background">
        <ObjectAnimationUsingKeyFrames>
            <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="#bb818c" />
            <DiscreteObjectKeyFrame KeyTime="0:0:0.9" Value="{x:Null}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</Border.Resources>
<i:Interaction.Triggers>
    <ei:DataTrigger Binding="{Binding LastClick}" Value="0"
            Comparison="GreaterThan">
        <ei:ControlStoryboardAction 
             Storyboard="{StaticResource Story}" ControlStoryboardOption="Play" />
    </ei:DataTrigger>
    <ei:DataTrigger Binding="{Binding LastClick}" Value="0"
            Comparison="LessThan">
        <ei:ControlStoryboardAction 
             Storyboard="{StaticResource Story2}" ControlStoryboardOption="Play" />
    </ei:DataTrigger>
</i:Interaction.Triggers></Border>