问题描述
使用 Visual Studio 2012 ulti 使用 WPF C#.NET4.5.
Using WPF C#.NET4.5 using visual studio 2012 ulti.
旧的 winforms 代码:
Old winforms code:
foreach (DataGridViewRow paretoRow in ParetoGrid.Rows)
{
if ((Convert.ToInt32(paretoRow.Cells["CurrentPareto"].Value) < (Convert.ToInt32(paretoRow.Cells["NewPareto"].Value))))
{
paretoRow.Cells["pNew"].Value = downArrow
}
}
如您所见,我循环遍历的每一行都会检查一个特定的单元格,如果为真,则填充另一个单元格.这是我以前使用过很多次的很好的旧 winforms 代码......但是.切换到 WPF 与我之前假设的完全不同.
As you can see each row I cycle through I check a specific cell, if true I then populate another cell. This was good old winforms code I used many times before...however.Switching over to WPF was alot more different than i previously assumed.
DataGrid
不包含 Row
属性.相反,我认为您需要使用:
DataGrid
does not contain the Row
property. Instead, I think you need to use:
DataGridRow paretoRow in paretogrid.Items
但我仍然不知道现在该由谁来获得细胞.
But im still at a loss on who to now get the cell.
所以我的问题是,是否有要执行的语法更改,如果有,在哪里?或者因为我开始相信 WPF 中的数据网格比 Winform 更能使用对象操作,因此不需要使用名为行"的属性,如果是这种情况,我应该知道在这个示例中使用什么逻辑/语法?
So my question is, is there syntax changes to perform, if so where? Or as I'm beginning to believe datagrids in WPF operate with Objects more so than winforms thus not needing to use a propertie called "row", if this is the case what logic/syntax should i know use in this example?
感谢你们的耐心等待,想想当我回家过银行假期时,我会做一些 WPF 挖掘,看看它实际上有多大不同.
Thanks for your patience guys, think when I go home for the bank holiday I'll do a bit of WPF digging to see how different it actually is.
推荐答案
我想你首先想到的是获取你的 DataGrid
的所有行:
I think first think you want to do is to get all rows of your DataGrid
:
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}
然后遍历您的网格:
var rows = GetDataGridRows(nameofyordatagrid);
foreach (DataGridRow row in rows)
{
DataRowView rowView = (DataRowView)row.Item;
foreach (DataGridColumn column in nameofyordatagrid.Columns)
{
if (column.GetCellContent(row) is TextBlock)
{
TextBlock cellContent = column.GetCellContent(row) as TextBlock;
MessageBox.Show(cellContent.Text);
}
}
这篇关于WPF 遍历数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!