WPF DataGrid not loading data -


i beginner in c# , wpf . have created user control logtable.atxml contains datagrid , added mainwindow.xaml . table displayed contents not being fetched. think issue im not able sent itemsource in right way. [result]please help.

   using system;    using system.collections.generic;    using system.linq;    using system.text;      namespace tabletest.usercontrols     {     class tabledata     {     string a{ get; set; }     string b { get; set; }     string c { get; set; }      public tabledata(string a, string b, string c)      {     = a;     b = b;     c =c;     }      }     }        namespace tabletest.usercontrols     {     /// <summary>     /// interaction logic logtable.xaml     /// </summary>     public partial class logtable : usercontrol     {      observablecollection<tabledata> list;     public logtable()     {     initializecomponent();     list = gettabledetails();     this.loggrid.itemssource = list;     }      private observablecollection<tabledata> gettabledetails()     {     observablecollection<tabledata> list= new observablecollection<tabledata>();     tabledata data = new tabledata("aaa", "aaa", "aaa");     tabledata data1 = new tabledata("bbb", "aaa", "aaa");     tabledata data2 = new tabledata("ccc", "aaa", "aaa");     list.add(data);     list.add(data1);     list.add(data2);     return list;     }     }     }      public partial class mainwindow : window     {     public mainwindow()     {     initializecomponent();     }     }     }   <window         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:usercontrols="clr-namespace:tabletest.usercontrols"       x:class="tabletest.mainwindow"         title="mainwindow" height="350" width="525">     <grid>          <usercontrols:logtable x:name="logtable" horizontalalignment="left" margin="0,209,0,0" verticalalignment="top" width="287" height="111"/>      </grid> </window>  <usercontrol x:class="tabletest.usercontrols.logtable"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               mc:ignorable="d"                >     <datagrid x:name="loggrid" autogeneratecolumns="false"            height="290"            horizontalalignment="left"            verticalalignment="top" width="290"               itemssource="{binding list}"           >         <datagrid.columns >             <datagridtextcolumn binding="{binding path=a}" minwidth="50" header="column 1"/>             <datagridtextcolumn binding="{binding path=b}" minwidth="50" header="column 2"/>             <datagridtextcolumn binding="{binding path=c}" minwidth="50" header="column 3"/>         </datagrid.columns>     </datagrid> </usercontrol> 

i think need few things 1. itemsource needs bind property. code should like

 public partial class logtable : usercontrol     {      public observablecollection<tabledata> list {get;set;}     public logtable()     {     initializecomponent();     datacontext=this;     list = new observablecollection<tabledata>();     list = gettabledetails();     this.loggrid.itemssource = list;     } 
  1. you need set data context of user control. if using codebehind can away setting datacontext in usercontrols constructor in code above. in future going want use mvvm pattern , set datacontext viewmodel.

note: need set datacontext of mainwindow if want access information window's codebehind (or whatever want bind data from).

here resource read on mvvm.

update: saw xaml. since named datagrid can away not setting datacontext setting itemsource directly in code. however, since don't have datacontext set can remove itemsource={binding list} xaml. work if have list property available on datacontext.

update 2: need make properties public on tabledata class. work

 class tabledata {     public string { get; set; }     public string b { get; set; }     public string c { get; set; }      public tabledata(string a, string b, string c)     {         = a;         b = b;         c = c;     }  } 

Comments