在Viewport中查找WPF控件

更新:

这可能是一个简单或复杂的问题,但在wpf中,我有一个列表框,我从列表中填入数据模板

有没有办法找出特定的datatemplate项目是否在视口中,即,我已滚动到其位置并且可以查看?

目前我迷上了listbox_ScrollChanged事件,这给了我ScrollChangedEventArgs,但是我找不到合适的属性……

任何帮助将不胜感激,谢谢!

看到这个问题

对于特定的ListBox,您可以执行此操作

private bool IsControlVisibleToUser(Control control) { ListBoxItem listBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(control) as ListBoxItem; if (listBoxItem != null) { return IsUserVisible(listBoxItem, listBox); } return false; } 

并且我从问题链接的方法

 private bool IsUserVisible(FrameworkElement element, FrameworkElement container) { if (!element.IsVisible) return false; Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight)); Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight); return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight); }