查找WPF控件中视 [英] Find WPF Controls in Viewport
问题描述
更新:
这可能是一个简单或复杂的问题,但在WPF中,我有我填了的的DataTemplate 的列表框从列表中。
This may be an easy or a complex question, but in wpf, I have a listbox that I fill in with a datatemplate from a list.
有没有办法找出如果一个特定的的DataTemplate项目的是在视口中也就是说,我已滚动到它的位置,它是可见的?
Is there a way to find out if a particular datatemplate item is in the viewport ie, I have scrolled to its position and it is viewable?
目前我迷上到listbox_ScrollChanged事件,这一点让我ScrollChangedEventArgs,但我还没有找到合适的物业...
Currently I hooked into the listbox_ScrollChanged event, and this gives me the ScrollChangedEventArgs, but I haven't found the right property...
任何帮助将是非常美联社preciated,谢谢!
Any help would be much appreciated, thanks!
推荐答案
请参阅<一href="http://stackoverflow.com/questions/1517743/in-wpf-how-can-i-determine-whether-a-control-is-visible-to-the-user">this问题
有关具体列表框,你可以做到这一点。
For a ListBox in specific you can do this
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);
}
这篇关于查找WPF控件中视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!