c# - WPF Bind ListBoxItem to DataGrid -
i need bind listboxitem , datagrid in following way: user doubleclicks on listboxitem , sql query performed , retrieved data shown on datagrid. here's have:
<datagrid x:name="gridstatistics" margin="0,144,0,0" /> <listbox x:name="lststat" horizontalalignment="left" height="129" margin="10,10,0,0" verticalalignment="top" width="330" fontsize="16" itemssource="{binding statisticsqueries}" cursor="arrow" previewmouserightbuttondown="lststat_previewmouserightbuttondown" previewmouserightbuttonup="lststat_previewmouserightbuttonup" mousedoubleclick="lststat_mousedoubleclick"> <listbox.itemtemplate> <datatemplate> <label content="{binding path=name}" fontweight="medium" fontsize="18" fontfamily="helveticaneuecyr"/> </datatemplate> </listbox.itemtemplate> <listbox.contextmenu> <contextmenu> <menuitem header="show" click="menuitem_onclick" /> </contextmenu> </listbox.contextmenu> </listbox>
and c# code:
public observablecollection<query> statisticsqueries {get{...}} private void lststat_mousedoubleclick(object sender, mousebuttoneventargs e) { if (lststat.selectedindex < 0) return; var item = lststat.selecteditem query; using (var connection = new mysqlconnection(databasemodel.connectionstring)) { connection.open(); if (item != null) using (var cmd = new mysqlcommand(item.text, connection)) { var dt = new datatable(); var adapter = new mysqldataadapter(cmd); adapter.fill(dt); gridstatistics.datacontext = dt; } connection.close(); } e.handled = true; }
and class query public class query : inotifypropertychanged
has properties used in code, no mistakes there.
although data retrieved correctly, datagrid doesn't react @ all. maybe there mistakes in binding or setting datacontext.
could please me correctly bind listboxitem doublemouseclick , datagrid datacontext? should create separate class that?
try : using (var cmd = new mysqlcommand(item.text, connection)) { . . gridstatistics.itemssource = dt.defaultview; }
Comments
Post a Comment