6

ListView で最初に表示されているアイテムを取得して ListViewItem を取得する方法を知っている人はいますか? インデックス 0 のアイテムを取得する方法は知っていますが、最初に表示されるアイテムは知りません。

4

5 に答える 5

4

これは仕事をするのにとても苦痛でした:

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;
    }
于 2010-05-28T08:02:12.410 に答える
3

同様のことを理解しようとした後、私はここで私の結果を共有すると思いました(他の応答よりも簡単に見えるので):

ここから得た簡単な視認性テスト。

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;
}
于 2012-09-28T14:18:58.580 に答える
2

リストボックスのオフセットを計算するだけで、最初に表示されるアイテムは、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;

これがお役に立てば幸いです。. .!

于 2013-01-06T01:04:48.227 に答える
2

もっと簡単な方法がないなんて信じられない...

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/2d527831-43aa-4fd5-8b7b-08cb5c4ed1db

于 2010-05-28T04:59:34.323 に答える