c# - Bind two pages in wpf -
i need populate combobox in page1 address details of selected name contained in combobox held in mainwindow page. have tried code below, combobox name in mainwindow not recognised.
mainwindow:
private void displayparts() { try { sc.open(); string query = "select * parts"; sqlcommand createcommand = new sqlcommand(query, sc); sqldatareader dr = createcommand.executereader(); while (dr.read()) { string name = dr.getstring(1); cbparts.items.add(name);//displaying list in combo box } sc.close(); } catch (exception ex) { messagebox.show(ex.message); } }
page1:
private void combobox_selectionchanged_1(object sender, selectionchangedeventargs e) { string constring = "data source=.;initial catalog=**.mdf;integrated security=true"; datacontext=mainwindow. string query = "select * partners name='" + cbparts.selecteditem.tostring() + "' ;"; sqlconnection condatabase = new sqlconnection(constring); sqlcommand cmddatabase = new sqlcommand(query, condatabase); sqldatareader myreader; try { sc.open(); myreader = cmddatabase.executereader(); if (myreader.read()) { txtpartner.text = myreader["name"].tostring(); } myreader.close(); sc.close(); } catch (exception ex) { messagebox.show(ex.message); } }
// may need cast mainwindow's type var mainwindow = application.current.mainwindow; //... mainwindow.cbparts.items.add(name);
additionally, bind combobox observablecollection
property, , add items collection.
Comments
Post a Comment