您好,欢迎访问一九零五行业门户网

WPF实现简单的进度条怎么做?

最近做一个项目,看到以前同事写的进度条效果不错,所以,拿来简化了下,不炫,但是项目中还是够用的。
还是,先来看下调用以后的效果
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;”是为了方便通过这是这个参数去样式文件里取样式的。
<lineargradientbrush x:key="foregroundcolor1" endpoint="1,0.5" startpoint="0,0.5"><gradientstop color="#ffbbf586" offset="0.5"/><gradientstop color="#ffd4f9c3" offset="1"/></lineargradientbrush><lineargradientbrush x:key="foregroundcolor2" endpoint="1,0.5" startpoint="0,0.5"><gradientstop color="#ff5be26e" offset="0.5"/><gradientstop color="#ff8dec9c" offset="1"/></lineargradientbrush><lineargradientbrush x:key="foregroundcolor3" endpoint="1,0.5" startpoint="0,0.5"><gradientstop color="#ffb656f2" offset="0.5"/><gradientstop color="#ffae8dfe" offset="1"/></lineargradientbrush><lineargradientbrush x:key="foregroundcolor4" endpoint="1,0.5" startpoint="0,0.5"><gradientstop color="#ff3ae9e9" offset="0.5"/><gradientstop color="#ff8dfdfe" offset="1"/></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实现简单的进度条怎么做?的详细内容。
其它类似信息

推荐信息