How can I set the binding of a DataGridTextColumn in code?

前端 未结 3 886
执念已碎
执念已碎 2021-02-19 18:45

I\'m using the toolkit:DataGrid from CodePlex.

I\'m generating the columns in code.

How can I set the equivalent of {Binding FirstName} in code?

3条回答
  •  借酒劲吻你
    2021-02-19 19:06

    The first answer about the new Binding is correct for me, too. The main problem to use that answer was that Binding belongs to four namespaces 8-(. The correct namespace is System.Windows.Data (.NET 4, VS2010). This leads to a more complete answer:

    dgtc.Binding = new System.Windows.Data.Binding("FirstName");
    

    A side note:

    In my case the context to set the binding was the iteration over the columns of the DataGrid. Before it is possible to change the binding it is necessary to cast the base class DataGridColumn to DataGridTextColumn. Then it is possible to change the binding:

    int pos = 0;
    var dgtc = dataGrid.Columns[pos] as DataGridTextColumn;
    dgtc.Binding = new System.Windows.Data.Binding("FirstName");
    

提交回复
热议问题