如何在代码中设置DataGridTextColumn的绑定?

时间:2022-10-15 07:27:24

I'm using the toolkit:DataGrid from CodePlex.

我正在使用CodePlex上的DataGrid。

I'm generating the columns in code.

我用代码生成列。

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

如何在代码中设置等价的{Binding FirstName} ?

Or alternatively, how can I just set the value, that's all I need to do, not necessarily bind it. I just want the value from my model property in the cell in the datagrid.

或者,我如何设置值,这是我需要做的,而不是绑定它。我只想要在datagrid中的单元格中的模型属性值。

DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = smartFormField.Label;
dgtc.Binding = BindingBase.Path = "FirstName"; //PSEUDO-CODE
dgtc.CellValue= "Jim"; //PSEUDO-CODE
CodePlexDataGrid.Columns.Add(dgtc);

3 个解决方案

#1


19  

Untested, but the following should work:

未经测试,但以下方法应该有效:

dgtc.Binding = new Binding("FirstName");

#2


6  

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:

关于新绑定的第一个答案对我来说也是正确的。使用这个答案的主要问题是绑定属于四个名称空间8-(。正确的命名空间是System.Windows。(数据。4,净VS2010)。这就引出了一个更完整的答案:

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:

在我的例子中,设置绑定的上下文是对DataGrid列的迭代。在更改绑定之前,有必要将基类DataGridColumn转换为DataGridTextColumn。然后可以更改绑定:

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

#3


2  

Example:

例子:

DataGridTextColumn dataColumn = new DataGridTextColumn();
dataColumn.Header = "HeaderName";
dataColumn.Binding = new Binding("HeaderBind");
dataGrid.Columns.Add(dataColumn); 

#1


19  

Untested, but the following should work:

未经测试,但以下方法应该有效:

dgtc.Binding = new Binding("FirstName");

#2


6  

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:

关于新绑定的第一个答案对我来说也是正确的。使用这个答案的主要问题是绑定属于四个名称空间8-(。正确的命名空间是System.Windows。(数据。4,净VS2010)。这就引出了一个更完整的答案:

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:

在我的例子中,设置绑定的上下文是对DataGrid列的迭代。在更改绑定之前,有必要将基类DataGridColumn转换为DataGridTextColumn。然后可以更改绑定:

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

#3


2  

Example:

例子:

DataGridTextColumn dataColumn = new DataGridTextColumn();
dataColumn.Header = "HeaderName";
dataColumn.Binding = new Binding("HeaderBind");
dataGrid.Columns.Add(dataColumn);