Sonhja
Sonhja

Reputation: 8448

Animate ProgressBar content WPF with specific value

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

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

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

Upvotes: 2

Views: 8499

Answers (3)

Vanlalhriata
Vanlalhriata

Reputation: 579

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

Upvotes: 11

Sonhja
Sonhja

Reputation: 8448

For those who are interested, I found a solution in this link. It explains how to fill ProgressBar value using a 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);
        }
    }
}

Upvotes: 0

Bolu
Bolu

Reputation: 8786

DoubleAnimation Constructor (Double, Duration) the first paramater is

The destination value of the animation.

So change this

DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);

to

DoubleAnimation doubleanimation = new DoubleAnimation(progressBar1.Value, duration);

Upvotes: 1

Related Questions