开发者

How do I bind the rows in my Datagrid to properties?

This is the cod开发者_如何学运维e in my viewmodel:

public DataView VariationGrid
{
    get
    {
        DataTable data = new DataTable();

        #region Columns
        foreach (var param in SelQuestion.QuestionParameters)
        {
            DataColumn datac = new DataColumn(param.Name);
            data.Columns.Add(datac);
        } 
        #endregion

        #region Rows
        foreach (Variation variation in SelQuestion.Variations)
        {
            DataRow datarow = data.NewRow();
            foreach (var parameter in variation.QuestionParameters)
            {
                datarow[parameter.Key.Name] = parameter.Value;
            }
            data.Rows.Add(datarow);
        } 
        #endregion

        return data.DefaultView;
    }
}

This I bind to the ItemsSource of my DataGrid.

Now I want to edit a cell, but how do I notify this to my property that his value has changed?


This is not the way to do binding because you are creating the object/collection locally and then lose track of it. This is very bad practice because on each get the collection will be recreated.

Load the data once (into an instance field/property).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜