简体   繁体   中英

Silverlight DataGrid and Binding columns

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

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 eg

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM