views:

23

answers:

1

Hi

I have a silverlight datagrid control and columns autogenerate property is set to false. I am using MVVM and wants to bind the columns collection.

The data which i get is from xml. Something similar to sample code

http://blogs.msdn.com/b/deepak_verma/archive/2008/07/19/dynamic-creation-binding-of-silverlight-datagrid.aspx

Now by passing the datagrid control from xmal file to the modelview I can get the expected behavior but with that way , i am adding columns in the datagrid control.

Is there any way, so that I can bind the columns collection with the datagrid control so that no need to pass the control to view model.

-Rajesh

A: 

Sounds like a strange implementation of MVVM you have there. Your ViewModel should be completely independent of the View. If you want to create dynamic columns on the grid then why not expose a relevant property collection on the ViewModel, and iterate through it in a relevant method on the View code behind e.g.

//Used with an Infragistics XamWebGrid control

private void BuildGrid()

{

foreach (var dataItem in ViewModel.MyDataCollection) {

var myCol = new TemplateColumn { HeaderText = dataItem.ItemNm, Key = dataItem.PrimaryKey };

MyGrid.Columns.Add(myCol);

}

}

You really shouldn't be passing controls from the View over to the ViewModel. The ViewModel should have no knowledge of any controls on the View.

Myles J