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?
Example:
DataGridTextColumn dataColumn = new DataGridTextColumn();
dataColumn.Header = "HeaderName";
dataColumn.Binding = new Binding("HeaderBind");
dataGrid.Columns.Add(dataColumn);
Untested, but the following should work:
dgtc.Binding = new Binding("FirstName");
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");