Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法获取WPF数据网格行_Wpf_C# 4.0_Prism_Wpfdatagrid_Avalondock - Fatal编程技术网

无法获取WPF数据网格行

无法获取WPF数据网格行,wpf,c#-4.0,prism,wpfdatagrid,avalondock,Wpf,C# 4.0,Prism,Wpfdatagrid,Avalondock,在我的wpf应用程序中,我每30分钟发布一个事件,在所有打开的窗口上重新绑定自动生成的datagrid,之后我循环遍历datagrid的每一行以执行一些操作。我使用下面的方法来获取自动生成的wpf数据网格的所有行 public static IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid) { var itemsSource = grid.ItemsSource as IEnumerable; if (nu

在我的wpf应用程序中,我每30分钟发布一个事件,在所有打开的窗口上重新绑定自动生成的datagrid,之后我循环遍历datagrid的每一行以执行一些操作。我使用下面的方法来获取自动生成的wpf数据网格的所有行

public static IEnumerable<DataGridRow> GetDataGridRows(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 DataGridRow;
       if (null != row) yield return row;
    }
}
公共静态IEnumerable GetDataGridRows(DataGrid grid) { var itemsSource=grid.itemsSource作为IEnumerable; 如果(null==itemsSource)产生返回null; foreach(itemsSource中的var项) { var row=grid.ItemContainerGenerator.ContainerFromItem(item)作为DataGridRow; 如果(空!=行)产生返回行; } } 对于活动窗口(活动选项卡),它可以正常工作,并返回所有行,但对于其他窗口(非活动选项卡),它返回null


(注意:在我的应用程序中,窗口是以选项卡格式排列的,我使用Avalon Dock作为停靠窗口控件。)

ItemContainerGenerator
将只返回可见行的行,因为默认情况下dataGrid是虚拟的。感谢Rohit的响应。对于这种情况,有什么方法/解决方法吗?@RohitVats是正确的。此外,我们通常不会在WPF中操作UI元素。相反,我们倾向于使用数据元素。请参阅MSDN上的页面了解更多信息。是否有任何方法/解决方案用于此场景?。。。不,不是按你现在的方式做的。解决方案是以WPF方式编写代码。请点击上面的链接了解更多信息。谢谢谢里登。我是wpf的新手。也许有更好的方法可以做到这一点。如果你能帮我的话,让我解释一下我的要求。我必须在datagrid中显示数据库表中的数据,并且显示的列数不是固定的,每次都会根据显示的数据类型而有所不同。假设A型有5列,B型可能有8列,以此类推。如果值为-ve,我还必须将每个单元格的前景色设置为红色。和单元格的背景色(如果其值与左侧列不同)。