使用特定值动画 ProgressBar 内容 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17594871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate ProgressBar content WPF with specific value
提问by Sonhja
I'm trying to develop a ProgressBarthat fills accordingly to a value that I manually set to it. For example, I have this ProgressBar:
我正在尝试开发一个ProgressBar根据我手动设置的值填充的值。例如,我有这个ProgressBar:
<ProgressBar Height="33" HorizontalAlignment="Left" Name="progressBar1" VerticalAlignment="Top" Width="285" />
I have a button that increases the ProgressBarvalue in 10 units each time you press it, like this:
我有一个按钮,ProgressBar每次按下它时都会增加10 个单位的值,如下所示:
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);
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%?
但它从 0 到 100 的值ProgressBar。如何让动画停止在特定值上,而不是 100%?
回答by Vanlalhriata
You are actually animating the Valueto a final value of 200 if you do th following:
Value如果您执行以下操作,您实际上是将 动画设置为 200 的最终值:
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);
}
回答by Bolu
DoubleAnimation Constructor (Double, Duration)the first paramater is
DoubleAnimation Constructor (Double, Duration)第一个参数是
The destination value of the animation.
动画的目标值。
So change this
所以改变这个
DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);
to
到
DoubleAnimation doubleanimation = new DoubleAnimation(progressBar1.Value, duration);
回答by Sonhja
For those who are interested, I found a solution in thislink. It explains how to fill ProgressBarvalue using a BackgroundWorker.
对于那些感兴趣的人,我在此链接中找到了解决方案。它解释了如何ProgressBar使用 BackgroundWorker.
I wrote this code:
我写了这段代码:
public MainWindow()
{
InitializeComponent();
backgroundworker.WorkerReportsProgress = true;
backgroundworker.WorkerSupportsCancellation = true;
backgroundworker.DoWork += backgroundworker_DoWork;
backgroundworker.ProgressChanged += backgroundworker_ProgressChanged;
}
private void buttonStop_Click(object sender, RoutedEventArgs e)
{
backgroundworker.CancelAsync();
e.Handled = true;
}
private void buttonStart_Click(object sender, RoutedEventArgs e)
{
if (backgroundworker.IsBusy == false)
{
backgroundworker.RunWorkerAsync();
}
e.Handled = true;
}
void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (worker != null)
{
for (int i = 0; i <= 10; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
System.Threading.Thread.Sleep(50);
worker.ReportProgress(i);
}
}
}

