use data trigger control the storyboard, but only trigger one time

谁都会走 提交于 2019-12-20 02:56:09

问题


I use a data trigger to control some storyboards, but it only can be trigger one time.

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

The para is a variable (already implement INotifyPropertyChanged interface)which will be control by another button. its value is 0 or 1.

But when I click the button to change the para value, the storyboard only trigger one time for every value(0 and 1). It will never trigger later.

If I put the second storyboard into ExitActions tag of the first data trigger. it will work well. But I have more than 2 storyboard need control......

following code works well,but I need control many storyboard (more than 2) according to different value....

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

回答1:


You can use System.Windows.Interaction.Interactivity with condition to launch storyboards.

xmlns:i="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>



回答2:


I had the same problem, then I gave Timeline.FillBehavior="Stop", which worked for me.

Check: Microsoft - Timeline Class Reference - Timeline.FillBehavior Property



来源:https://stackoverflow.com/questions/7410464/use-data-trigger-control-the-storyboard-but-only-trigger-one-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!