1

My List<BusinessObject> has some public properties that I want to bind to columns in a DataGrid. Unfortunately, the names of the public properties are not good and I may not even know what they are until runtime. For this reason, I set AutoGenerateColumns=True and interecept each DataGridAutoGeneratingColumnEvent so I can inspect what it is and either cancel it, hide it, or name the header something else.

It works great but I cannot figure out how to set the Mode=TwoWay so that my INotifyPropertyChanged events get fired once all the columns are generated and somebody edits a cell.

Bonus question: On navigating up and down the rows of the grid, does the grid's datacontext automatically get set with that row's BusinessObject?

4

2 回答 2

0

如果绑定正确,您的业务对象将自动接收所需的更新。要以编程方式进行绑定,您可能需要更多代码,例如:

...
Binding binding = new Binding("Propertyname");
tc.binding.Mode = BindingMode.TwoWay;
...
于 2009-11-26T13:15:48.907 回答
0

感谢这篇文章,我了解到绑定发生在 DataGridTextColumn 上。所以在运行时设置Mode的方法是:

1    private void DataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
2    {
3        DataGridTextColumn tc = (DataGridTextColumn)e.Column;
4        tc.Header = "Custom Header";
5        tc.Binding.Mode = BindingMode.TwoWay;
6    }

现在我有了 TwoWay 绑定,我必须弄清楚更改如何使它返回到我的 BusinessObject。

于 2009-11-03T23:14:43.640 回答