wpf - C# ListBox not showing added items when added from DispatcherTimer.Tick -
this looks weird behaviour me , since not blame else myself spent hours trying fix - not understand @ all:
i noticed problem occures in method called dispatchertimer.tick event. multi-thread issue.
i have listbox:
<listbox itemssource="{binding configurationerrors, mode=oneway}"/>
it binds to:
private observablecollection<string> _configurationerrors = new observablecollection<string>(); public observablecollection<string> configurationerrors { { return _configurationerrors; } } /// <summary> /// adds configuration error window. /// </summary> /// <param name="errormessage">the message add.</param> public void addconfigurationerror(string errormessage) { if(string.isnullorempty(errormessage)) return; _configurationerrors.add(errormessage); notifypropertychanged("configurationerrors"); } /// <summary> /// removes configuration errors window. /// </summary> public void clearconfigurationerrors() { _configurationerrors.clear(); notifypropertychanged("configurationerrors"); }
addconfigurationerror(string errormessage)
sucessfully adds message if called
from anywhere in mainwindow
.
(from constructor , anywhere else)
and have looping method (called dispatchertimer.tick) in instance stored in
app.cs
, contains following code://file exists if (configfilepath == null) { _mainwindow.addconfigurationerror("could not retrieve config filepath."); throw new invaliddataexception("could not retrieve config filepath."); } else if (!file.exists(configfilepath)) { _mainwindow.addconfigurationerror("could not find config. (" + configfilepath + ")"); throw new invaliddataexception("could not find config. (" + configfilepath + ")"); }
the exceptions being thrown , addconfigurationerror()
being called. can log message passed addconfigurationerror()
, works, - control not recieve binding update.
is because dispatchertimer.tick runs in different thread , bindings may not work expected way wrote it? , how can fix it?
thank in advance.
call methods on proper mainwindow instance. haven't shown how create _mainwindow
, instead of calling
_mainwindow.addconfigurationerror(...);
you should call
((mainwindow)application.current.mainwindow).addconfigurationerror(...);
Comments
Post a Comment