最近做一个项目,看到以前同事写的进度条效果不错,所以,拿来简化了下,不炫,但是项目中还是够用的。
还是,先来看下调用以后的效果
1、因为ProgressbBar的Foreground显示不得不一样,所以,要有一个参数去给控件进行设置,因此定义了一个参数值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; }}
登录后复制
代码里有这么一句话“LinearGradientBrush lgb = dictionary[“ForegroundColor” + value] as LinearGradientBrush;”是为了方便通过这是这个参数去样式文件里取样式的。
登录后复制
2、既然是ProgressBar就要有一个进度值,这个值,我们用TextBlock来进行显示,一定要实现通知接口,这样,才能保证实时的通知到页面上。
- public string ValueText{get{return _valueText; }set{ _valueText = value;if (this.PropertyChanged != null) {this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ValueText")); } }}
登录后复制
3、启用一个后台线程,来不断的更新进度效果
- 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 + "";}
登录后复制
源码
以上就是WPF实现简单的进度条怎么做?的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。