使用特定值对ProgressBar内容WPF进行动画处理 [英] Animate ProgressBar content WPF with specific value

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

问题描述

我正在尝试开发一个 ProgressBar ,以相应地填充我手动为其设置的值.例如,我有这个 ProgressBar :

I'm trying to develop a ProgressBar that fills accordingly to a value that I manually set to it. For example, I have this ProgressBar:

<ProgressBar Height="33" HorizontalAlignment="Left" Name="progressBar1" VerticalAlignment="Top" Width="285" />

我有一个按钮,每次按下该按钮时,它会以10个单位增加 ProgressBar 的值,就像这样:

I have a button that increases the ProgressBar value in 10 units each time you press it, like this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    progressBar1.Value += 10;
}

每次单击该值时,我都希望为其设置动画.我试过了:

I want to animate that value change each time I click on it. I tried this:

Duration duration = new Duration(TimeSpan.FromSeconds(1));
DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
progressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);

但是它从 ProgressBar 的值到0到100.如何告诉动画停止在特定值而不是100%?

But it goes from 0 to 100 value of the ProgressBar. How can I tell the animation to stop on a specific value, instead of going to 100%?

推荐答案

如果执行以下操作,则实际上是在将 Value 设置为最终值200的动画:

You are actually animating the Value to a final value of 200 if you do th following:

DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);

相反,将第一个参数更改为要设置为动画值的值.您的事件处理程序应如下所示:

Instead change the first argument to the value you want to animate to. Your event handler should be like so:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Duration duration = new Duration(TimeSpan.FromSeconds(1));
    DoubleAnimation doubleanimation = new DoubleAnimation(progressBar1.Value + 10, duration);
    progressBar1.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
}

这篇关于使用特定值对ProgressBar内容WPF进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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