我有一些奇怪的行为,我似乎无法解决。当我迭代 ListBox.ItemsSource 属性中的项目时,我似乎无法获取容器?我期望看到返回一个 ListBoxItem,但我只得到 null。
有任何想法吗?
这是我正在使用的代码:
this.lstResults.ItemsSource.ForEach(t =>
{
ListBoxItem lbi = this.lstResults.ItemContainerGenerator.ContainerFromItem(t) as ListBoxItem;
if (lbi != null)
{
this.AddToolTip(lbi);
}
});
ItemsSource 当前设置为字典并且包含许多 KVP。
我在这个 StackOverflow 问题中发现了一些更适合我的情况的东西:
获取数据网格中的行
通过在调用 ContainerFromItem 或 ContainerFromIndex 之前放入 UpdateLayout 和 ScrollIntoView 调用,您可以实现 DataGrid 的该部分,从而可以返回 ContainerFromItem/ContainerFromIndex 的值:
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
如果您不希望更改 DataGrid 中的当前位置,这对您来说可能不是一个好的解决方案,但如果可以的话,它无需关闭虚拟化即可工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)