任何人都知道如何通過抓取ListView中的第一個可見項來獲取ListViewItem?我知道如何獲得索引0的項目,但不是第一個可見的項目。在WPF ListView中獲取第一個可見項C#
回答
這是如此的痛苦得到工作:
HitTestResult hitTest = VisualTreeHelper.HitTest(SoundListView, new Point(5, 5));
System.Windows.Controls.ListViewItem item = GetListViewItemFromEvent(null, hitTest.VisualHit) as System.Windows.Controls.ListViewItem;
和函數來獲取列表item:
System.Windows.Controls.ListViewItem GetListViewItemFromEvent(object sender, object originalSource)
{
DependencyObject depObj = originalSource as DependencyObject;
if (depObj != null)
{
// go up the visual hierarchy until we find the list view item the click came from
// the click might have been on the grid or column headers so we need to cater for this
DependencyObject current = depObj;
while (current != null && current != SoundListView)
{
System.Windows.Controls.ListViewItem ListViewItem = current as System.Windows.Controls.ListViewItem;
if (ListViewItem != null)
{
return ListViewItem;
}
current = VisualTreeHelper.GetParent(current);
}
}
return null;
}
在試圖找出類似的東西之後,我想我會我的結果在這裏(因爲它似乎比其他反應更容易):
簡單的能見度測試我從here得到。
private static 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));
var rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}
之後,您可以遍歷listboxitems並使用該測試來確定哪些是可見的。由於listboxitems總是按照相同的順序排列,因此列表中的第一個可見列表項將成爲用戶的第一個可見列表項。
private List<object> GetVisibleItemsFromListbox(ListBox listBox, FrameworkElement parentToTestVisibility)
{
var items = new List<object>();
foreach (var item in PhotosListBox.Items)
{
if (IsUserVisible((ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(item), parentToTestVisibility))
{
items.Add(item);
}
else if (items.Any())
{
break;
}
}
return items;
}
我們只有計算偏移我們的列表框,並且第一個可見項目將是該指數等於VerticalOffset在項目......
// queue is the name of my listbox
VirtualizingStackPanel panel = VisualTreeHelper.GetParent(queue.Items[0] as ListBoxItem) as VirtualizingStackPanel;
int offset = (int)panel.VerticalOffset;
// then our desired listboxitem is:
ListBoxItem item = queue.Items[offset] as ListBoxItem;
希望這有助於你。 。 。!
我收到錯誤:'發生了'System.InvalidCastException'類型的未處理異常。 我認爲它出現在鑄造listBox.Items [0] – thowa 2015-12-20 16:24:22
我想它不起作用的情況下分組,但否則它是fastes方式(到目前爲止) – SnowballTwo 2016-08-15 13:05:29
- 1. NativeScript ListView。獲取第一個可見項目的位置iOS
- 2. 僅在Android ListView中獲取第一項?
- 3. ListView第一項不可見/更新Android
- 4. 如何從Android ListView獲取第一個可見視圖
- 5. android listview第一個可見位置
- 6. WPF ListView選定的項目可見
- 7. 獲取的一個ListView C#
- 8. winforms listview - 使第一列始終可見?
- 9. RecyclerView先保留第一個可見項
- 10. Flex-獲取列表中的第一個可見元素
- 11. 如何在第一個ListView中選擇一行後顯示(可見)第二個ListView?
- 12. 如何在C#中將不可見項添加到ListView中?
- 13. Android-ListView的最後一項不可見
- 14. C#/ WPF:從一個ListView獲取所選行
- 15. 能夠從WPF/C#中的選中項目中獲取ListView項目的列表?
- 16. Android ListView:獲取可見項目的數據索引
- 17. 如何獲取ExpandableListView中第一個/最後一個可見組的索引?
- 18. 如何獲取jCarouselLite中的可見項?
- 19. WPF ListView滾動條可見爲false
- 20. C#wpf listview添加項目
- 21. 更改combobox中第一個滾動中可見的項目數
- 22. 獲取哪個進程/窗口實際上在C#中可見#
- 23. C#可摺疊的ListView WPF
- 24. DOJO DataGrid - 如何獲取第一個可見行和最後一個可見行的索引(編號)
- 25. vimscript獲取第一個和最後一個可見行的編號
- 26. C#/ WPF:使GridViewColumn可見= false?
- 27. 在WPF中獲取可見的控件大小
- 28. C#在詳細信息視圖中從listview中的第一項抓取文本
- 29. 如何獲取ListView中的開始索引和可見項目的數量?
- 30. 獲取IEnumerable項的第一個元素
哦,我的上帝,我想我只是吐在我的嘴裏。如何使用VisualTreeHelper來HitTest相對點0,0處的孩子? – Josh 2010-05-28 05:01:47
使用HitTest使其工作。感謝您的建議! – 2010-06-01 02:02:42