0

I've created a TreeViewItem which has a ProgressBar in it.

<DataTemplate>
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="35" MinWidth="100">
         <CheckBox Name="chk" IsChecked="True" Margin="2" Tag="{Binding}" Style="{DynamicResource myCheckBoxStyle}" />
         <TextBlock Text="{Binding}" Padding="0 10 0 10" Width="100" />
         <ProgressBar x:Name="tvProgressBar" HorizontalAlignment="Right" Width="300" Height="20" Foreground="#FF089ACE"  />
      </StackPanel>
</DataTemplate>

But I can not seem to update the value of the progressbar, I can find the progress bar by doing

ProgressBar pb = (System.Windows.Controls.ProgressBar)(tv.HeaderTemplate.LoadContent() as StackPanel).FindName("tvProgressBar");

But when I update the value of the progress bar the UI is not updated. If I create a ProgressBar outside of the TreeView is updates as expected so it seems to be the way I'm either accessing the ProgressBar embedded in the TreeViewItem or the way it needs to be updated.

  • Try with default values for `MinValue` and `MaxValue` properties maybe setting to `MinValue=1`, `MaxValue=100`. Could not exactly recall property name is `MinValue` or `MinimumValue`. – Siva Gopal Aug 05 '17 at 18:28
  • Minimum and Maximum values do not make a different, thanks anyways – Chris Richardson Aug 05 '17 at 19:21
  • There are only two reasons your progress bar might not visually update: **1)** you are in fact _not_ actually changing the `Value`, or **2)** you are attempting to show progress of an operation that is being executed in the UI thread. The correct way to use `ProgressBar` is to run your long-running operation in a background thread (e.g. `Task.Run()`), and have that operation update a _view model_ property, which is bound to the `ProgressBar.Value` property. You didn't post a good [mcve] that reliably reproduces the problem, so it's not possible to actually answer the question. – Peter Duniho Aug 06 '17 at 05:17
  • I believe the issue I have is the progressbar in the template needs to be databound to an object. I configured the process to run from another thread and doing a watch on the progressbar I can clearly see the object is updated with the values I have set and are increasing but no update is drawn to the UI. It seems (System.Windows.Controls.ProgressBar)(tv.HeaderTemplate.LoadContent() as StackPanel).FindName("tvProgressBar") does not allow you to update the object. – Chris Richardson Aug 06 '17 at 07:59

1 Answers1

0

How are you updating the value? Do you get the "wait" cursor? If so you probably need to use a worker thread to run the UI on so that the progress bar actually updates and doesn't just go from 0 to 100.

Check out these links on the progress bar control : 1 , 2, 3

Essentialy, try this :

private readonly BackgroundWorker worker = new BackgroundWorker { WorkerReportsProgress = true };

public MainWindow()
{
    InitializeComponent();

    worker.DoWork += worker_DoWork;
    worker.ProgressChanged += worker_ProgressChanged;
}

private void worker_DoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
    // Do some long process, break it up into a loop so you can periodically
    //  call worker.ReportProgress()

    worker.ReportProgress(i);  // Pass back some meaningful value
}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    prgBar.Value = Math.Min(e.ProgressPercentage, 100);
}
Tsimp. Dim
  • 76
  • 13
  • The UI is responsive and does not freeze up, the progress bar just never updates. I've tried in another thread using a dispatcher.BeingInvoke but still get the same issue. – Chris Richardson Aug 05 '17 at 18:38
  • I tried adding a progressbar outside of the TreeViewItem template and it updates as expected so its something to do with it being in the template. – Chris Richardson Aug 05 '17 at 19:32