Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Progress Bar Animation Speed

I've noticed that there is a difference in the time it takes for a WPF Progress Bar and a WinForms Progress Bar to fill completely.

Fill completely as in set the Value to 100 in both Forms and WPF, one can notice that WinForms fills the bar smoothly whereas the WPF fills it instantly.

I wanted to know if there is a property that we can edit in the templates to change that.

Hope I made it clear, I can post a video too if anyone wants.

EDIT

Here's a video of what I'm talking about, notice the difference ?

EDIT 2

Filling the progress bar with a timer ?

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.Title = "WPF Progress Bar Demo";
        }

        private void fill(int from, int to)
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(0.5));
            DoubleAnimation doubleanimation = new DoubleAnimation(from, to, duration);
            progb.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);
        }

        private void fill_Click(object sender, RoutedEventArgs e)
        {
            fill(0, 100);
        }
    }
}

Is that OK and will it work anywhere ?

Feel free to change it.

Thanks.

like image 626
vs2010noob Avatar asked Oct 10 '22 00:10

vs2010noob


1 Answers

The idea is that a progress bar reports actual progress - not time elapsed. It's not intended to be an animation that just indicates something is happening.

The basic principle is that you bind Value to a property on your DataContext class, and update that value whenever a progress milestone occurs.

You can make it fill at a specified rate using a timer - here is an example:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ProgressBar Value="{Binding Path=ProgressValue}"></ProgressBar>
</Grid>

And the code:

  public partial class MainWindow : Window, INotifyPropertyChanged
{
    Timer timer;
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
        timer.Start();
    }

    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (this._progressValue < 100)
            this.ProgressValue = _progressValue + 10;
        else
        {
            timer.Stop();
            timer.Dispose();
        }
    }

    private double _progressValue;
    public double ProgressValue
    {
        get { return _progressValue; }
        set
        {
            _progressValue = value;
            RaisePropertyChanged("ProgressValue");
        }
    }

    private void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
like image 138
Greg Sansom Avatar answered Oct 19 '22 12:10

Greg Sansom