WPF ProgressBar Visibility with MVVM does not Work -
i have wpf window displays list of records between 2 dates. use : mvvm light, entity framework , stored procedure.
when run command display list, want display progress bar indicate task running. when query finished want hide progress bar. problem visibility of progress bar not work well. below code:
//xaml . . . . <statusbar grid.row="2"> <statusbaritem width="300"> <textblock text="{binding sbmessage, updatesourcetrigger=propertychanged, mode=twoway}"/> </statusbaritem> <statusbaritem width="auto"> <grid> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <textblock text="requête en cours..." visibility="{binding taskinprogress, updatesourcetrigger=propertychanged, mode=oneway, converter={staticresource booltovisibility}}" /> <progressbar visibility="{binding taskinprogress, updatesourcetrigger=propertychanged, mode=oneway, converter={staticresource booltovisibility}}" width="100" height="20" isindeterminate="true" verticalalignment="center" grid.column="1" /> </grid> </statusbaritem> </statusbar> //viewmodel bool _taskinprogress = false; public bool taskinprogress { { return _taskinprogress; } set { _taskinprogress = value; raisepropertychanged("taskinprogress"); } } public relaycommand displaysimulationslistcommand { { if (_splist == null) _splist = new relaycommand(displaysimulationslistcommandexecute); return _splist; } } private void displaysimulationslistcommandexecute() { sbmessage = "exécution..."; taskinprogress = true; stopwatch stopwatch = new stopwatch(); stopwatch.start(); doitwithstoredprocedure(); stopwatch.stop(); timespan ts = stopwatch.elapsed; string elapsedtime = string.format("{0:00}:{1:00}:{2:00}.{3:00}", ts.hours, ts.minutes, ts.seconds, ts.milliseconds / 10); sbmessage = listsimulations.count().tostring() + " enregistrement(s) en : " + elapsedtime; currentdisplayedtab = 1; taskinprogress = false; //sbmessage = "prêt"; } private void doitwithstoredprocedure() { try { using (unitofwork cx = new unitofwork()) { var ls = cx.getsimulationsperiode(vmperiode.debut, vmperiode.fin).asreadonly(); listsimulations = new observablecollection<simulation>(ls); cvs = (listcollectionview)collectionviewsource.getdefaultview(listsimulations); raisepropertychanged("cvs"); } } catch (exception ex) { messenger.default.send<exceptionmessagerefresh>(new exceptionmessagerefresh(ex), "doitwithstoredprocedure"); } } //converter public class booltovisiblityconverter : ivalueconverter { #region constructors /// <summary> /// default constructor /// </summary> public booltovisiblityconverter() { } #endregion public bool collapse { get; set; } #region ivalueconverter members public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { bool bvalue = (bool)value; if (bvalue) return visibility.visible; else { if (collapse) return visibility.collapsed; else return visibility.hidden; } } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { visibility visibility = (visibility)value; if (visibility == visibility.visible) return true; else return false; } #endregion }
thank in advance.
the accepted answer how did in old days (pre-.net framework 4), when children have walk uphill to/from school. here more readable solution doesn't require asinine amount of code (thanks .net framework 4/4.5).
private void doitwithstoredprocedure() { //do normal sp stuff here //instead, mocked synchronous method thread.sleep(1000); console.writeline("completed"); } private void displaysimulationslistcommandexecute() { //do stuff console.writeline("started"); taskinprogress = true; task.run(() => doitwithstoredprocedure()) .continuewith( task => { taskinprogress = false; }); console.writeline("finished"); //do rest of stuff }
output:
started //showed progressbar animation finished //task.run hit completed //progressbar became collapsed after continuewith hit
proof:
as can see, own eyes, accepted method of using task.run
, continuewith
1-line solution , can read , understood easily.
Comments
Post a Comment