c# - Designing my models for binding to DataGrid items and headers -
i'm trying obtain datagrid in view data in model. think must reorganize data in viewmodel display them in view. i'm new in mvvm , i'd understand 2 points of question:
- taking in mind not know neither number of rows, neither number of columns, kind of data holder should use? observablecollection<>? more in detail each row have item, , each column have aspect of item.
- how can set headers of datagrid?
the rows should apperar 1,2,3... columns should have specific names.
this try:
mainwindow.xaml:
<datagrid autogeneratecolumns="false" itemssource="{binding mytable}" /> mainwindowviewmodel.cs:
private observablecollection<list<myitem>> _mytable; public class myitem { public myitem(string value) { value= value; } public string value{ get; set; } } public observablecollection<list<myitem>> mytable { { return _mytable; } set { _mytable = value; raisepropertychanged("mytable"); } } the execute of comand is:
private void displaydatatableexecute() { mytable = new observablecollection<list<myitem>>(); var temp = new list<myitem>(); temp.add(new myitem("one")); temp.add(new myitem("two")); temp.add(new myitem("three")); mytable.add(temp); temp = new list<myitem>(); temp.add(new myitem("one")); temp.add(new myitem("two")); temp.add(new myitem("three")); mytable.add(temp); } when use autogeneratecolumns="false" see rows (the correct number empty) when use autogeneratecolumns="true" see properties of table (capacity , count).
i need help, because can't find solution.
there 2 main issues here:
first: can't use autogeneratecolumns = "false" without setting columns. you'll need map these through yourself, ie:
<datagrid autogeneratecolumns="false" itemssource="{binding mytable}" > <datagrid.columns> <datagridtextcolumn header="value" binding="{binding value}"/> </datagrid.columns> </datagrid> note can set autogeneratecolumns="true" instead, give 1 column per property in class.
second: need make class (class myitem) implement inotifypropertychanged if want have changes made in code reflected in ui.
Comments
Post a Comment