How to implement a simple progress bar in WPF?

零下一度
Release: 2017-06-23 15:17:55
Original
4453 people have browsed it

I was working on a project recently, and I saw that the progress bar written by a former colleague was very effective, so I simplified it. It is not dazzling, but it is still sufficient for the project.

Still, let’s take a look at the effect after the call.

1. Because the Foreground display of ProgressbBar must be the same, there must be a parameter to Control is set, so a parameter value ForegroundColor

public int ForegroundColor {get{return _foregroundColor; }set{ _foregroundColor = value; LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;if (lgb != null) proBar.Foreground = txt.Foreground = percent.Foreground = lgb; } }
Copy after login

is defined. There is such a sentence in the code "LinearGradientBrush lgb = dictionary["ForegroundColor" + value] as LinearGradientBrush;" for the convenience of passing This is the parameter used to get the style from the style file.

Copy after login

2. Since it is a ProgressBar, it must have a progress value. We use TextBlock to display this value. We must implement the notification interface to ensure real-time notification to the page. superior.

public string ValueText {get{return _valueText; }set{ _valueText = value;if (this.PropertyChanged != null) {this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ValueText")); } } }
Copy after login

3. Enable a background thread to continuously update the progress effect

private void Bgw_DoWork(object sender, DoWorkEventArgs e) {for (int i = 0; i < BarValue; i++) { System.Threading.Thread.Sleep(50); proBar.Dispatcher.Invoke(new Action( delegate{if (proBar.Value <= BarValue) { proBar.Value++; } })); ValueText = i + ""; } ValueText = BarValue + ""; }
Copy after login

Source code

The above is the detailed content of How to implement a simple progress bar in WPF?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!