WPF动画与datatriggers [英] WPF Animation with datatriggers

查看:84
本文介绍了WPF动画与datatriggers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的动画底色色的矩形。

I have a rectangle which I am animating the backround colour of.

应当每一个特定的号码上升时间变为绿色。红色当它的承。如果号码不改变一会儿就慢慢变淡恢复到默认的颜色

It should change to green each time a particular number goes up. And red when it goes dowm. If the number doesn't change for a while it slowly fades back to its default colour

所以动画改变了从灰色到红很快届背景,然后花费几秒钟的渐退为灰色。

So the animation changes th background from grey to red very quickly and then takes several seconds to fade back to grey.

我已经加入这势必为1或-1 DataTrigger取决于如何号码已更改,

I have added as DataTrigger which is bound to 1 or -1 depending on how the number has changed

问题是,如果数量不断上升的动画不会被重新启动。

The problem is that if the number keeps going up the animation does not get restarted.

例如。如果数字的顺序去了1,2,3,4,5,那么我想在动画的每个数字的变化来重新启动

e.g. if the sequence of numbers went 1, 2, 3, 4, 5. Then I would like the animation to restart at each number change

在code我现在用的就是以下

the code I am using is below

<Rectangle.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding BidChangeDirectionIndicator}"
                         Value="-1">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="bidDownStory">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                <EasingColorKeyFrame KeyTime="0"
                                                     Value="#FF79797A" />
                                <EasingColorKeyFrame KeyTime="0:0:0.2"
                                                     Value="#FFF13B29" />
                                <EasingColorKeyFrame KeyTime="0:0:10.0"
                                                     Value="#FF79797A" />
                            </ColorAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                <EasingColorKeyFrame KeyTime="0"
                                                     Value="#FF2B2B2B" />
                                <EasingColorKeyFrame KeyTime="0:0:0.2"
                                                     Value="#FF3F0606" />
                                <EasingColorKeyFrame KeyTime="0:0:10.0"
                                                     Value="#FF2B2B2B" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="bidDownStory" />
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>

和视图模型我结合看起来像这样

and the ViewModel I am binding to looks like this

private int _bidChangeDirectionIndicator;
public int BidChangeDirectionIndicator
{
    get { return _bidChangeDirectionIndicator; }
    set
    {
        _bidChangeDirectionIndicator = value;
        RaisePropertyChanged("BidChangeDirectionIndicator");
    }
}

....

public void Update(RateInfo rateInfo)
{
    if (rateInfo.Bid != Bid)
    {
        BidChangeDirectionIndicator = Math.Sign(rateInfo.Bid - Bid);
    }            
}

该方法被每一次叫号的变化(这是由它收听外部饲料类来完成)

The method gets called each time the number changes (this is done by a class which is listening to an external feed)

推荐答案

[告诫:我还没来得及重建和测试。我只是觉得这可能会有帮助。]

[Caveat: I have not had time to recreate and test this. I just thought it may help.]

的原因,不会引发触发器是该值不会改变。当你,例如,继续往上走,BidChangeDirectionIndicator的值保持为1。

The reason that the trigger does not fire is that the value does not change. When you, for instance, keep going up, the value of BidChangeDirectionIndicator remains 1.

也许,最简单的做法是将其设置为其他值(0),然后到你想要的值。

Perhaps the simplest thing to do is to set it to another value (0), and then to the value you want.

public void Update(RateInfo rateInfo)
{
    if (rateInfo.Bid != Bid)
    {
        BidChangeDirectionIndicator = Math.Sign(rateInfo.Bid - Bid);
    }            
    else
    {
        BidChangeDirectionIndicator = 0;
        BidChangeDirectionIndicator = rateInfo.Bid;
    }
}

这篇关于WPF动画与datatriggers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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