通过WPF数据网格迭代 [英] WPF iterate through datagrid

查看:181
本文介绍了通过WPF数据网格迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WPF C#.NET4.5使用Visual Studio 2012中的大招

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 不包含属性。相反,我认为你需要使用:

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 DataGrid中使用对象操作更比的WinForms所以不需要使用所谓的行propertie,如果是这样的话是什么逻辑/语法应我知道在这个例子中使用?

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;
    }
}



,然后通过网格迭代:

and then iterate through your grid:

 var rows= GetDataGridRows(nameofyordatagrid); 

foreach (DataGridRow r in rows)  
  {  
    DataRowView rv = (DataRowView)r.Item;
    foreach (DataGridColumn column in nameofyordatagrid.Columns)
    {
        if (column.GetCellContent(r) is TextBlock)
        {
            TextBlock cellContent = column.GetCellContent(r) as TextBlock;
            MessageBox.Show(cellContent.Text);
        }
  } 

这篇关于通过WPF数据网格迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆