title: Using progress bar in WPF C# MVVM
body: <p>My goal is to update the progress bar while another set of script (calculations) is running.</p>
  
  <p>I have followed the sample files from <a href="https://www.wpf-tutorial.com/misc-controls/the-progressbar-control/" rel="nofollow noreferrer">here</a> and tried to bind it to my MVVM script but the progress bar would not update.</p>
  
  <p><a href="https://i.stack.imgur.com/AmMmQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AmMmQ.png" alt="enter image description here"></a></p>
  
  <p><strong>Here is the Progressbar script</strong></p>
  
  <p>In the script below, I have included <code>progressBarCounter</code> and <code>noOfData</code>as a value in another script that is calculated in a method.</p>
  
  <p>Proof that data is updated</p>
  
  <p><a href="https://i.stack.imgur.com/REhCY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/REhCY.png" alt="enter image description here"></a></p>
  
  <pre><code>public partial class ProgressBarTaskOnWorkerThread : Window
  {
      public ProgressBarTaskOnWorkerThread()
      {
          InitializeComponent();
      }
  
      private void Window_ContentRendered(object sender, EventArgs e)
      {
          BackgroundWorker worker = new BackgroundWorker();
          worker.WorkerReportsProgress = true;
          worker.DoWork += worker_DoWork;
          worker.ProgressChanged += worker_ProgressChanged;
  
          worker.RunWorkerAsync();
      }
  
      void worker_DoWork(object sender, DoWorkEventArgs e)
      {
          EtabsDataFormatting.ViewModel.SpliceViewModel data = new EtabsDataFormatting.ViewModel.SpliceViewModel();
  
          for (int i = data.progressBarCounter; i &lt; data.noOfData;)
          {
              (sender as BackgroundWorker).ReportProgress(i);
          }
      }
  
      void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          pbStatus.Value = e.ProgressPercentage;
          int perc = Convert.ToInt32(pbStatus.Value);
          UpdateProgress(perc);
      }
  
      public void UpdateProgress(int percentage)
      {
          pbStatus.Value = percentage;
          if (percentage == 100)
          {
              Close();
          }
      }
  }
  </code></pre>
  
  <p><strong>Here is part of my XAML code for the button to start calculations and run the progressbar</strong></p>
  
  <p>The command Binding = RunCalcBtn is bound to the calculation scripts, therefore, I have created a click to run the progress bar instead.</p>
  
  <pre><code>&lt;Button x:Name = "ApplyButton" Margin="0 1 0 1" Content ="Start Calculation" Command="{Binding RunCalcBtn, Mode=TwoWay}" Click ="PrgBar_Click"/&gt;
  </code></pre>
  
  <p><strong>Progressbar XAML.cs button click</strong></p>
  
  <p>This part displays the progress bar, but it does not update.</p>
  
  <pre><code>private void PrgBar_Click(object sender, RoutedEventArgs e)
  {
      ProgressBar.ProgressBarTaskOnWorkerThread progressWindow = new ProgressBar.ProgressBarTaskOnWorkerThread();
      progressWindow.Show();
  }
  </code></pre>
  
  <p>Thank you so much for helping me in advance!</p>