Skip to main content
 首页 » 编程设计

c#之如何以编程方式绑定(bind)数据网格列

2024年02月13日12grandyang

我一定是忘记了一些明显的事情......我无法以编程方式绑定(bind) DataGridColumn。我有以下代码:

        var datagrid = new DataGrid(); 
        datagrid.Columns.Add(new DataGridTextColumn() 
        { 
            Header = "Type, 
            Width = new DataGridLength(200), 
            FontSize = 12, 
            Binding = new Binding("Name") // this line fails to complile 
        }); 

我不断收到以下编译错误:

Error 3 The type or namespace name 'Binding' could not be found (are you missing a using directive or an assembly reference?)

我在网上看到很多例子,似乎都在做同样的事情......有什么想法吗? 谢谢,

请您参考如下方法:

var datagrid = new DataGrid(); 
datagrid.Columns.Add(new DataGridTextColumn() 
{ 
    Header = "Type, 
    Width = new DataGridLength(200), 
    FontSize = 12, 
    Binding = new Binding("Name") 
}); 

为了使用这些代码,您必须使用命名空间“System.Windows.Data”。

using System.Windows.Data;