我正在为需要在 WPF 上使用节点和连接器的项目编码。我的节点和连接器类是
public class Node
{
private string _nodeId;
private string _nodeType;
private double _x;
private double _y;
/*
set/get function here
*/
}
public class Connector
{
private string _startNodeId;
private string _endNodeId;
/*
set/get function here
*/
}
我想使用数据网格列出所有连接器,如下所示
-----------------------------------------
| | 01 | 02 | 03 | 04 |
-----------------------------------------
| 01 | x | 1 | 0 | 1 |
-----------------------------------------
| 02 | 0 | x | 0 | 1 |
-----------------------------------------
| 03 | 1 | 1 | x | 1 |
-----------------------------------------
| 04 | 1 | 1 | 0 | x |
-----------------------------------------
在数据网格上,列和行标题是节点列表中的节点 ID。所以我需要手动将行添加到数据网格。我搜索了很多,但没有运气。
谁能帮我。谢谢!
为什么不将数据网格的data source
创建为数据表,然后在代码隐藏中分配源代码。请参阅下面的代码。
// Create a datatable for your datagrid's source
DataTable datatbl = new DataTable();
datatbl.Columns.Add("startNode_ID", typeof(int));
datatbl.Columns.Add("endNode_ID", typeof(int));
// You can add new rows to datatable here
for (int iCount = 1; iCount < MaximumRequiredCount; iCount++)
{
var row = datatbl.NewRow();
row["startNode_ID"] = Your Start Node ID;
row["endNode_ID"] = Your End Node ID;
datatbl.Rows.AddRow(row);
}
DataGridView dataGrid1 = new DataGridView();
dataGrid1.AutoGenerateColumns = true;
dataGrid1.DataSource = datatbl;
在 xaml 中设置绑定,如下所示
<DataGrid Name="dataGrid1" ItemsSource="{Binding}">