使用数据触发器控制情节提要,但只触发一次 [英] use data trigger control the storyboard, but only trigger one time

查看:27
本文介绍了使用数据触发器控制情节提要,但只触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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

但是当我单击按钮更改 para 值时,故事板仅针对每个值(0 和 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.

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

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

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

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>

推荐答案

您可以使用 System.Windows.Interaction.Interactivity 和条件来启动故事板.

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"

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>

这篇关于使用数据触发器控制情节提要,但只触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆