Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a
C#
List<List<MyClass>>
which is basically a nXm matrix with each node being an instance on MyClass.

I want to display this in a datagrid (WPF , MVVM). I have done this in WinForms C#(Event Driven Programming) and I want to rewrite this in WPF using MVVM pattern. I am not clear as to how to do this in MVVM pattern.
Posted
Comments
rohith naik 22-Mar-13 11:21am    
Members of MyClass :
String MyText;
List (tuple(int,int,int)) reference;
//Each node references other nodes. On selection of a node the other referenced nodes must be highlighted. the tuple is of the format "row, column, referencetype"

1 solution

Do you have a specific reason for using a DataGrid?

The way your data is designed, I would think an <ItemsControl> would serve to get the grid you are looking for. You could use a <UniformGrid> element which would cause all of the 'nodes' to be the same size.

There is a sample of using <ItemsControl> and <UniformGrid> here[^]
 
Share this answer
 
Comments
rohith naik 23-Mar-13 6:02am    
The data represented in the matrix is a real table(example http://corpeconresearch.files.wordpress.com/2009/08/corpecon-philipppine-stocks-guide-sample-3_page_4.jpg). Each node has a reference to its column header and row header. There is no advantage of having as equally sized grid. I want to be able to edit and modify the data in the table and add new rows / columns. Hope this clarifies why i want to use a Data Grid
rohith naik 23-Mar-13 6:41am    
And again another problem is that the number of rows and columns are not fixed. I have another assembly which sends me this data.
seventhnight 25-Mar-13 9:09am    
Editing the data and adding rows fits within the MVVM pattern very simply. However, it might be clearer to think of things in terms of more classes instead of just the List<> objects.

For example, it you construct a class that represents the entire 'table', you can create a AddRow Command to create a new row Entry. Also from the table, you could expose the 'rows' collection as an ObservableCollection<rowdata> or something similar. This is what you would bind to your DataGrid's ItemsSource. The binding of the columns is a little trickier since you want them to be unknown. I suspect you would have to override DataGrid to allow you to dynamically add columns. I'll try to update my solution with a better example shortly
rohith naik 25-Mar-13 13:33pm    
I do have a Table Class as you say. I have simplifies the problem here a bit so that I could get a generic answer. I have found a nice example of adding properties dynamically here "http://www.shujaat.net/2012/09/dynamically-generated-properties-using.html" which I was planning to use but I could not find a way to bind the columns dynamically. Maybe a ObservableCollection ?
Thanks for taking the time to help me out - appreciated
rohith naik 25-Mar-13 13:37pm    
I would have named the properties "column1", "column2" and so on so that I could dynamically generate the property names based on the row or column number.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900