为什么ItemContainerGenerator.ContainerFromIndex()返回null,以及如何避免这种情况? [英] Why ItemContainerGenerator.ContainerFromIndex() returns null and how to avoid this behavior?

查看:1918
本文介绍了为什么ItemContainerGenerator.ContainerFromIndex()返回null,以及如何避免这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个片段来分析,我在一个DataGrid选择的行。

I'm using this snippet to analyze the rows I've selected on a datagrid.

for (int i = 0; i < dgDetalle.Items.Count; i++)
{
    DataGridRow row = (DataGridRow)dgDetalle.ItemContainerGenerator.ContainerFromIndex(i);
    FrameworkElement cellContent = dgDetalle.Columns[0].GetCellContent(row);
    // ... code ...
}

周期运行平稳,但处理某些索引时,第二行会引发空异常。 MSDN文档说, ItemContainerGenerator.ContainerFromIndex(我)如果'如果该项目没有实现,但这并不能帮助我猜我怎么能拿将返回null所需的值。

The cycle runs smoothly, but when processing certain indexes, the second line throws a null exception. MSDN's documentation says that ItemContainerGenerator.ContainerFromIndex(i) will return null if 'if the item is not realized', but this doesn't help me to guess how could I get the desired value.

我如何可以扫描所有的行?有没有其他办法?

How can I scan all the rows? Is there any other way?

更新

我用这个片段读复选框如下解释 。所以我不能用绑定或 ItemSource 水平,除非我改变了很多东西。而我不能。我做code维修。

I'm using this snippet to read a CheckBox as explained here. So I can't use binding or ItemSource at all unless I change a lot of things. And I cannot. I'm doing code maintenance.

推荐答案

的DataGrid 是虚拟化的项目,各行(即集装箱)仅创建行时在视图。

The DataGrid is virtualizing the items, the respective rows (i.e. containers) are only created when the row is in view.

您既可以<一href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.isvirtualizing.aspx"相对=nofollow>关闭虚拟化(这使得第一次加载,如果你有很多的项目,也是内存使用量会高一些很慢),或者你只是遍历数据和检查数据的值对象的属性应绑定到数据网格。通常你不应该在所有需要的UI元素...

You could either turn off virtualization (which makes the first time loading very slow if you have many items, also the memory usage will be higher) or you just iterate over the data and check the values of the data objects' properties which should be bound to the data-grid. Usually you should not need the UI elements at all...

这篇关于为什么ItemContainerGenerator.ContainerFromIndex()返回null,以及如何避免这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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