ViewModel first in a DataGridCell

Apr 28, 2013 at 4:24 AM
I’m attempting to populate a Datagrid.

The Datagrid has a variable number of columns which is defined by a collection of ViewModels.
My first issue was how can I create these columns using MVVM. (i.e. create the columns in the parent ViewModel) I could not figure out how to do this so I went with a solution where I am creating the columns in Code behind on the Data Grid loading event.

This works fine I can add a new column to the datagrid and assign a DataTemplate to the cell.
foreach (ViewModels.CellViewModel cellViewModel in dataGridRowModel.DataGridCellModelCollection)
             DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
             dgtc.CellTemplate = Resources["MachineDayTemplate"] as DataTemplate;
This works fine for assigning the view, but I am struggling where I can introduce the view model. I want to use MeffedMvvm because I want to import some IContextAware Services

I initially tried to do a view first approach where I was successful in creating a view and the view model was also created and attached. But I could not inject any data into the viewmodel? The viewModel need to be assigned a date, and a numeric Id.

My second attempt is to apply ViewModel first as I would like to inject MeffedMVVM capabilities. But I can still not figure out how I can do this with a DataGrid cell?

The DataContext needs to come from my ViewModel Collection.

I have also thought of using Workspace, but I cannot see the answer?

Can someone provide me with a few suggestions to help me move forward?