gpt4 book ai didi

c# - 在 WPF ListView C# 中获取第一个可见项

转载 作者:太空狗 更新时间:2023-10-29 22:24:49 25 4
gpt4 key购买 nike

有人知道如何通过抓取 ListView 中的第一个可见项来获取 ListViewItem 吗?我知道如何获取索引 0 处的项目,但不知道第一个可见的项目。

最佳答案

开始工作太痛苦了:

HitTestResult hitTest = VisualTreeHelper.HitTest(SoundListView, new Point(5, 5));
System.Windows.Controls.ListViewItem item = GetListViewItemFromEvent(null, hitTest.VisualHit) as System.Windows.Controls.ListViewItem;

获取列表项的函数:

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;
}

关于c# - 在 WPF ListView C# 中获取第一个可见项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2926722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com