Tuesday, January 24, 2012

ProgressBar in WPF

A progress is used to indicates the progress of an operation. I've searched a lot on the Internet. I couldn't find a sample on how to use it by setting its progress value. And should be the simplest and most natural way to do it.

PrograssBar class in MSDN it only told you how to use it by calling BeginAnimation method or how to use a Storyboard by embedding it into your XAML file (right in the Example section) or calling BeginStoryboard method.

Although, you can customize your ProgressBar Control by creating a ControlTemplate. Here are the parts and states that are specific to the ProgressBar: ProgressBar Styles and Templates.

The simplest way to make the ProgressBar work is to set its IsInderterminate property by setting it to either false or true. Doing this will not only reset the ProgressBar's animation behavior on either ways but also start the progress feedback. In MSDN library and information that all over the Internet only told you that this property (IsIndeterminate) is tend to "Gets or sets whether the ProgressBar shows actual values or generic, continuous progress feedback". (I was wrong about it after I read and test it again. I don't know how I ended wrong while I write my code but below is the right answer and the easiest way. I may affect by the wrong articles on the Internet during that time.)

The simplest way to make the ProgressBar work is to set its Value porperty. Here is a example in how to:

You will need to have a ProgressBar contorl named progressBar1 and two Button controls named Start_Button and Stop_Button in your .xaml fils.

   1:  namespace ProgressBarSample
   2:  {
   3:      /// <summary>
   4:      /// Interaction logic for MainWindow.xaml
   5:      /// </summary>
   6:      public partial class MainWindow : Window
   7:      {
   8:          private BackgroundWorker _backgroundWorker = new BackgroundWorker();
   9:   
  10:          public MainWindow()
  11:          {
  12:              InitializeComponent();
  13:   
  14:              // Set up the BackgroundWorker.
  15:              this._backgroundWorker.WorkerReportsProgress = true;
  16:              this._backgroundWorker.WorkerSupportsCancellation = true;
  17:              this._backgroundWorker.DoWork += new DoWorkEventHandler(bw_DoWork);
  18:              this._backgroundWorker.ProgressChanged +=
new ProgressChangedEventHandler(bw_ProgressChanged);
  19:          }
  20:   
  21:          private void Start_Button_Click(object sender, RoutedEventArgs e)
  22:          {
  23:              if (this._backgroundWorker.IsBusy == false)
  24:              {
  25:                  this._backgroundWorker.RunWorkerAsync();
  26:              }
  27:              e.Handled = true;
  28:          }
  29:   
  30:          private void Stop_Button_Click(object sender, RoutedEventArgs e)
  31:          {
  32:              this._backgroundWorker.CancelAsync();
  33:              e.Handled = true;
  34:          }
  35:   
  36:          void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  37:          {
  38:              // Set the Value porperty when porgress changed.
  39:              this.progressBar1.Value = (double)e.ProgressPercentage;
  40:          }
  41:   
  42:          void bw_DoWork(object sender, DoWorkEventArgs e)
  43:          {
  44:              BackgroundWorker _worker = sender as BackgroundWorker;
  45:              if (_worker != null)
  46:              {
  47:                  for (int i = 1; i <= 100; i++)
  48:                  {
  49:                      if (_worker.CancellationPending == true)
  50:                      {
  51:                          e.Cancel = true;
  52:                          break;
  53:                      }
  54:                      else
  55:                      {
  56:                          System.Threading.Thread.Sleep(250);
  57:                          _worker.ReportProgress(i);
  58:                      }
  59:                  }
  60:              }
  61:          }
  62:   
  63:      }
  64:  }

The above code is a modified copy from Shell_TaskbarItemSample in MSDN.


5 comments: