获取WPFListView中第一个可见项C#有谁知道如何通过抓取ListView中第一个可见项获取ListViewItem?我知道如何获取索引0处的项目,但不知道第一个可见项目。上面的类很蛋疼:HitTestResulthitTest=VisualTreeHelper.HitTest(SoundListView,newPoint(5,5));System.Windows.Controls.ListViewItemitem=GetListViewItemFromEvent(null,hitTest.VisualHit)asSystem.Windows.Controls.ListViewItem;和获取列表项的函数:System.Windows.Controls.ListViewItemGetListViewItemFromEvent(objectsender,objectoriginalSource){if(depObj!=null){//在视觉层次结构中向上移动,直到我们找到点击来自的列表项//点击可能在网格或列标题上,所以我们需要满足这个DependencyObjectcurrent=depObj;while(current!=null&¤t!=SoundListView){System.Windows.Controls.ListViewItemListViewItem=currentasSystem.Windows.Controls.ListViewItem;if(ListViewItem!=null){返回ListViewItem;}current=VisualTreeHelper.GetParent(current);}}返回空值;在尝试找出类似我将在这里分享我的结果的东西之后(因为它看起来更多他的回答更简单):我从这里得到的简单可见性测试矩形边界=element.TransformToAncestor(container).TransformBounds(newRect(0.0,0.0,element.ActualWidth,element.ActualHeight));varrect=newRect(0.0,0.0,container.ActualWidth,container.ActualHeight);返回rect.Contains(bounds.TopLeft)||rect.Contains(bounds.BottomRight);之后,您可以遍历listboxitems并使用该测试来确定哪些是可见的。由于listboxitems总是排序相同,因此该列表中第一个可见的将是第一个对用户可见的。私有列表GetVisibleItemsFromListbox(ListBoxlistBox,FrameworkElementparentToTestVisibility){varitems=newList();foreach(PhotosListBox.Items中的varitem){if(IsUserVisible((ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(item)){.Add(item);}elseif(items.Any()){break;}}返回项目;}我不敢相信没有更简单的方法...http://social.msdn.microsoft.com/forums/en-US/wpf/thread/2d527831-43aa-4fd5-8b7b-08cb5c4ed1db我们只是需要计算列表框的偏移量,第一个可见项是索引等于VerticalOffset的项...//queue是我的列表框的名称VirtualizingStackPanelpanel=VisualTreeHelper.GetParent(queue.Items[0]asListBoxItem)asVirtualizingStackPanel;intoffset=(int)panel.VerticalOffset;//那么我们想要的列表框项是:ListBoxItemitem=queue.Items[offset]asListBoxItem;这可以帮助你......!以上就是C#学习教程:GetthefirstvisibleiteminWPFListViewC#分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文来自网络收集,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
