ok, trying use filesystemwatcher watch changes in directory , dynamically change items in combo box. populatecb() method works when user control loaded. , added break point inside watcher changed event breaks when change contents of directory. know events being triggered , watcher_changed method being called. contents of combo-box not change... doing wrong?
public partial class htmlviewer : usercontrol { private list<string> emails = new list<string>(); filesystemwatcher watcher = new filesystemwatcher("emailtemplates", "*.msg"); public htmlviewer() { initializecomponent(); populatecb(); watcher.changed += watcher_changed; watcher.created += watcher_changed; watcher.deleted += watcher_changed; watcher.renamed += watcher_changed; watcher.enableraisingevents = true; } void watcher_changed(object sender, filesystemeventargs e) { this.dispatcher.invoke((action)(() => { populatecb(); } )); } private void populatecb() { emails.clear(); foreach(var file in directory.enumeratefiles("emailtemplates", "*.msg", searchoption.alldirectories)) { emails.add(file); } emailselector.itemssource = emails; } }
the simplest solution set itemsource null before changing list<string> emails
private void populatecb() { emailselector.itemssource = null; emails.clear(); foreach(var file in directory.enumeratefiles("emailtemplates", "*.msg", searchoption.alldirectories)) { emails.add(file); } emailselector.itemssource = emails; } without not changing previous itemsource. same object. list<string> has no capability notify binding infrastructure of wpf of changes elements. clearing items, readding them totally invisible combobox. instead setting itemsource = null, , resetting again informs combobox of changes.
a possible (and better option) change list observablecollection has capability notifiy changes
Comments
Post a Comment