【问题标题】:How can I set the binding of a DataGridTextColumn in code?如何在代码中设置 DataGridTextColumn 的绑定?
【发布时间】:2010-10-29 07:45:23
【问题描述】:

我正在使用 CodePlex 的工具包:DataGrid。

我正在代码中生成列。

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

或者,我怎样才能设置值,这就是我需要做的,不一定绑定它。我只想要数据网格中单元格中模型属性的值。

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

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    未经测试,但以下应该可以工作:

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

    【讨论】:

      【解决方案2】:

      例子:

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

      【讨论】:

        【解决方案3】:

        关于新绑定的第一个答案对我来说也是正确的。使用该答案的主要问题是 Binding 属于四个命名空间 8-(。正确的命名空间是 System.Windows.Data (.NET 4, VS2010)。这导致了更完整的答案:

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

        附注:

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

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

        【讨论】:

          猜你喜欢
          • 2011-11-23
          • 2012-10-16
          • 1970-01-01
          • 2020-10-14
          • 1970-01-01
          • 2012-10-14
          • 1970-01-01
          • 2011-12-21
          • 2014-09-13
          相关资源
          最近更新 更多