0
我是WPF的新手,尝试将程序从WinForms转换为WPF。我遇到了一个呃试图从DataGridView转换到DataGrid并添加行。任何帮助表示赞赏。向WPF DataGrid添加空行,然后编辑单元格
原始代码:
for (int i = 0; i < noteArray.Count; i++)
{
int newIndex = dtgrdNotes.Rows.Add();
dtgrdNotes.Rows[newIndex].Cells[0].Value = noteArray[i].ToString();
dtgrdNotes.Rows[newIndex].Cells[1].Value = chkbx1.Checked;
dtgrdNotes.Rows[newIndex].Cells[2].Value = chkbx2.Checked;
}
我需要的代码在WPF工作,我已经试过的dtgrdNotes.Items甚至DataRowView的不同的变化,但迄今没有奏效。有什么建议么?
更新* XAML,与控制问题无二理查德:
<DataGrid Name="dtgrdNotes" Height="178" Width="739" HorizontalAlignment="Right" VerticalAlignment="Bottom" Canvas.Left="1" IsEnabled="False" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="dgtxtbxNote" Width="*" Binding="{Binding note}"/>
<DataGridCheckBoxColumn x:Name="dgchbxInquire" Width="100" Binding="{Binding inquire}" />
<DataGridCheckBoxColumn x:Name="dgchbxPrint" Width="100" Binding="{Binding print}" />
<DataGridTemplateColumn x:Name="dgbtncDelete" Width="80" Header="Button">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="dgbtncDeleteButton" Content="Delete" Width="50px" Height="10px" FontWeight="Bold" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
所以,在这个例子中,我可以通过创建一个新的“人”,然后补充说,人可观察集合添加一个新行,每当我想要的吗? – Hank
是的。一个好处是,如果您编辑视图,集合会自动查看更改 – Richard
我不确定是否应该为此提出新问题,但也许您会知道。我试图实现类似于你的例子,但与列中的内容是控件(复选框,按钮等)每次我尝试,我得到出现在单元格中是“System.WIndows.Controls.CheckBox “或”System.WIndows.Controls.Button“而不是实际的控制。我已经正确地更改了类变量类型,并且在将它添加为DataGrid ItemsSource之前,它确实显示为我的类的一部分。该列在XAML中也适用于复选框。有任何想法吗? – Hank