带数据触发器的 WPF 动画

发布于 2024-10-10 13:51:10 字数 2936 浏览 2 评论 0原文

我有一个矩形,我正在为其背景颜色设置动画。

每次特定数字上升时,它都应该变为绿色。当它下降时呈红色。如果数字一段时间内没有改变,它会慢慢淡出回默认颜色,

因此动画会非常快速地将背景从灰色更改为红色,然后需要几秒钟的时间淡回灰色。

我添加了 DataTrigger,它根据数字的变化情况绑定到 1 或 -1

问题是,如果数字持续上升,动画不会重新启动。

例如,如果数字序列变为 1、2、3、4、5。那么我希望动画在每个数字更改时重新启动,

我正在使用的代码如下

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

,我绑定到的 ViewModel 看起来像这样

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);
    }            
}

该方法被调用每次数字发生变化时(这是由正在监听外部源的类完成的)

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.

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.

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-10-17 13:51:10

[警告:我没有时间重新创建和测试它。我只是认为这可能会有所帮助。]

触发器不触发的原因是该值没有改变。例如,当您继续向上时,BidChangeDirectionIndicator 的值仍为 1。

也许最简单的方法是将其设置为另一个值 (0),然后设置为您想要的值。

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

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

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.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文