Reputation: 1673
I've encountered a problem trying to add items to WPF DataGrid. I want to load a matrix of M x N at runtime and place all the elements in the grid. However I've found only solutions that use ItemSource property or Binding for columns and as I understand they do not cope, because I need to create a predefined class. How could I accomplish that?
Upvotes: 0
Views: 684
Reputation: 49974
Column binding is only used when AutoGenerateColumns is set to false, so you've declaratively specified the exact columns you want and therefore have to also specify where they get their data from. (Good tutorial here).
You should set the datagrid's ItemsSource
to an IEnumerable of objects - this means you can use a List, an array of your objects, or a straight DataTable.
If you need to be totally dynamic with the columns in the datagrid, then either set AutoGenerateCOlumns to true, or write some logic to programmatically create and add the columns when appropriate.
Upvotes: 1