c# - while binding DataGrid WPF
i trying bind observablecollection
datagrid
.
code
private void loaddata_loaded(object sender, routedeventargs e) { observablecollection<loaddata> loaddataset = new observablecollection<loaddata>(); var items = new list<loaddata>(); loaddata load = new loaddata("1", "1", "1", "1"); loaddataset.add(load); items.add(load); loaddatagrid.itemssource = items; }
in mainwindow
public mainwindow() { initializecomponent(); datacontext = this; }
in xaml
datagrid name="loaddatagrid" horizontalalignment="left" margin="373,83,0,0" verticalalignment="top" height="64" width="661" loaded="loaddata_loaded" /
then exception
an unhandled exception of type "system.stackoverflowexception" occurred in xxxx.exe
why not working ?
while can't imagine have done wrong, can show how right. create public property collection:
private observablecollection<loaddata> items; public observablecollection<loaddata> items { { return items; } set { items = value; notifypropertychanged("items"); } } // implement inotifypropertychanged interface here!!
then data bind collection property datagrid.itemssource
property:
<datagrid name="loaddatagrid" itemssource="{binding items}" horizontalalignment="left" margin="373,83,0,0" verticalalignment="top" height="64" width="661" />
then finally, populate collection:
items = new observablecollection<loaddata>(); loaddata load = new loaddata("1", "1", "1", "1"); loaddataset.add(load); items.add(load);
Comments
Post a Comment