c# - 以编程方式将列和行添加到 WPF Datagrid

标签 c# wpf datagrid

我想知道我们应该如何以编程方式将列和行添加到 WPF 中的 DataGrid。我们以前在 Windows 窗体中使用的方式。创建表列和行,并将其绑定(bind)到 DataGrid。

我需要在 DataGrid 中绘制行数和列数,以便用户可以编辑单元格中的数据。

最佳答案

以编程方式添加一行:

DataGrid.Items.Add(new DataItem());

以编程方式添加列:

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "First Name"; 
textColumn.Binding = new Binding("FirstName"); 
dataGrid.Columns.Add(textColumn); 

查看 this post在 WPF DataGrid 讨论板上获取更多信息。

关于c# - 以编程方式将列和行添加到 WPF Datagrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/704724/

相关文章:

WPF OneWayToSource 绑定(bind)初始值

html - WPF:显示存储在资源程序集中的基于 HTML 的内容

.net - 从 View 和模型更新的 MVVM 数据网格

wpf - 数据表列表/数组问题

c# - 使用 OpenXML 将 Word docx 转换为 Excel

c# - 在 .NET Compact 中模拟曲线图

c# - 我应该为此使用线程吗?

c# - 如何让双击编辑在我的 ListView 中的一行上工作?

c# - 如果需要选择数据网格的行,请使用复选框列

c# - 刷新 DB First EDMX 时如何保留 StoreGeneratedPattern 设置?