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

前端 未结 3 807
执念已碎
执念已碎 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

    Example:

    DataGridTextColumn dataColumn = new DataGridTextColumn();
    dataColumn.Header = "HeaderName";
    dataColumn.Binding = new Binding("HeaderBind");
    dataGrid.Columns.Add(dataColumn); 
    
    0 讨论(0)
  • 2021-02-19 19:06

    Untested, but the following should work:

    dgtc.Binding = new Binding("FirstName");
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
提交回复
热议问题