c# - ListBox does not update entries automaticly when using binding -
im having trouble updating listbox when new data added it. code
the listbox has
<listbox name="emaillist" itemssource="{binding listboxdata, mode=twoway}"
and here program:
public partial class mainwindow : window { string hostname = properties.settings.default.pop_host; int port = properties.settings.default.pop_port; bool usessl = properties.settings.default.pop_usessl; string username = "recent:" + properties.settings.default.username; string password = properties.settings.default.password; // when button pressed program starts backgroundworker // begins download of mails private void menuitem_click(object sender, routedeventargs e) { backgroundworker getnewmail = new backgroundworker(); getnewmail.dowork += newemail; getnewmail.runworkerasync(); getnewmail.runworkercompleted += updatelist; } // actual function downloads mails (using openpop) private void newemail(object sender, doworkeventargs e) { list<message> allemail = fetchallmessages(hostname, port, usessl, username, password); listboxdata = new list<emailentry> { }; foreach (message singleemail in allemail) { var maildata = new listboxdataclass { themessage = singleemail, truncate = 40 }; readyuplistboxdata(maildata); listboxdata.add(new emailentry { = maildata.displayname, subject = maildata.partofbody, messageid = singleemail.headers.messageid.tostring() }); } } public class listboxdataclass { public message themessage { get; set; } public int truncate { get; set; } public string partofbody { get; set; } public string displayname { get; set; } } // function different things downloaded data public void readyuplistboxdata(listboxdataclass data) { messagepart theemailtxt = data.themessage.findfirstplaintextversion(); string nolinebreaks = theemailtxt.getbodyastext().tostring().replace(system.environment.newline, " "); data.partofbody = nolinebreaks.length <= data.truncate ? nolinebreaks : nolinebreaks.substring(0, data.truncate) + " .."; data.displayname = data.themessage.headers.from.displayname.tostring(); if (data.displayname == "") { data.displayname = data.themessage.headers.from.address.tostring(); } data.displayname += " <" + data.themessage.headers.from.address.tostring() + ">"; } }
i believe im supposed use called observable collection or that? cannot see how can use here in program. hope of guys can assist me in usage of observable collection or point me else can use, need.
i thinking using timer achieve it, im not sure practice ?
create public property listboxdata follows:
public partial class mainwindow : window { public observablecollection<emailentry > listboxdata{get;set;} public mainwindow() { listboxdata = new observablecollection<emailentry >(); initializecomponents(); } private void newemail(object sender, doworkeventargs e) { list<message> allemail = fetchallmessages(hostname, port, usessl, username, password); foreach (message singleemail in allemail) { var maildata = new listboxdataclass { themessage = singleemail, truncate = 40 }; readyuplistboxdata(maildata); listboxdata.add(new emailentry { = maildata.displayname, subject = maildata.partofbody, messageid = singleemail.headers.messageid.tostring() }); } }
Comments
Post a Comment