如何使用WPF进度条?

我试图使用WPF进度条控件,并将IsIndeterminate属性设置为true。 我遇到的问题是它没有得到更新。

我正在做这样的事情:

pbProgressBar.Visibility = Visibility.Visible; //do time consuming stuff pbProgressBar.Visibility = Visibility.Hidden; 

我尝试将其包装在一个线程中,然后使用Dispatcher对象进行调度。 我应该如何解决这个问题:)。

您必须在后台线程上执行耗时的操作,并且必须确保在后台线程完成其操作之后Visibility不会重新设置为Hidden 。 基本流程如下:

 private void _button_Click(object sender, RoutedEventArgs e) { _progressBar.Visibility = Visibility.Visible; new Thread((ThreadStart) delegate { //do time-consuming work here //then dispatch back to the UI thread to update the progress bar Dispatcher.Invoke((ThreadStart) delegate { _progressBar.Visibility = Visibility.Hidden; }); }).Start(); }