wpf - C# - Doubled data in ComboBox (SQLAnywhere connection) -
i write tool has got connection sql anywhere database via connectionstring. want receive info , bind them combobox via loaded event.
private void vesselnocombobox_loaded(object sender, routedeventargs e) { listshipnames(); }
/// <summary> /// receive vessels database , add together them combobox. /// variable database connection has built. /// </summary> private void listshipnames() { saconnection sqlcon = new saconnection(); sqlcon.connectionstring = "data source=****; user id=****; password=****"; seek { sqlcon.open(); sacommand cmd = new sacommand("select id || ' ' || name test.ship_data", sqlcon); sadatareader reader = cmd.executereader(); while (reader.read()) { vesselnocombobox.items.add(reader.getstring(0)); } } grab (exception ex) { messagebox.show(ex.message, "error ", messageboxbutton.ok, messageboximage.exclamation); } { sqlcon.close(); } }
in wpf first combobox_loaded executed when starting program. , when usercontrol loaded. info doubled. explicit clear()
remove combobox entries. give me hint?
without farther info regarding solution, cannot give reason why. can tell loaded event can fired multiple times when command loaded within template. because loaded event fired every time command included in visual tree reason can infered msdn blog mike hillberg.
note: may not know beingness loaded template. example, tab command treats each tab template, loading when click on tab , unloading when move off. philipp sumi describes problem here.
the way around ensure info loading code not in command loading event.
a more wpf solution utilize databinding. instead of loaded info , adding straight control. this,
set info context view model. populate observablecollection, perchance in constructor of viewmodel. bind itemssource property of combobox collection.i hope helps.
c# wpf combobox wpf-controls sqlanywhere
No comments:
Post a Comment