我正在使用WPF应用程序 . 我需要使用一些属性动态创建按钮,然后在dataGridView Rows中添加按钮 . 但是Datatable不包含Button DataType那么怎么做请有人提前帮助我 .
这是我的代码:
DataTable NewDataTable = new DataTable();
for (int i = 0; i < Row * level; i++)
{
Button[] buttonArray = new Button[position];
string Col1 = "Rows";
string Col2 = "Levels";
for (int j = 0; j < position; j++)
{
if (j == 1)
{
Col1 = "Rows";
}
else if (j == 2)
{
Col2 = "Levels";
}
else
{
buttonArray[j] = new Button();
buttonArray[j].Content = "Postion " + j;
buttonArray[j].ToolTip = "Postion " + j;
}
}
NewDataTable.Rows.Add(Col1, Col2, buttonArray[j]);
}
代码背后
Dgrid.ItemsSource = NewDataGrid.DefaultView();
2 回答
听起来你还没有遇到DataGridTemplateColumn Class . 让我向您介绍可以包含其中任何控件的列...来自链接页面:
一个例子,再次来自链接页面:
我创建了一个工作示例 . 下面是代码
这是datagrid的Xaml部分 .
代码背后的文件
正如您所看到的,我为您的按钮创建了一个新类 . 如果直接向
DataGrid
添加按钮,则会遇到问题 . 额外的课程非常简单请检查它是否有效 .
由于我没有做任何实施,你必须处理 property 变化