Search code examples
wpflistviewlistviewitemadorneradornerlayer

Determining if a ListViewItem is fully or only partially visible in the WPF ListView


I have a WPF ListView. This ListView will contain n-items.

As a user drags something into the listview, I am displaying a horizontal bar on the AdornerLayer of the ListView to show the index where the item will be inserted.

I also am auto-scrolling the listView. Because ScrollViewer.CanContentScroll="False" ListViewItems are partially visible.

The problem I am having is that the indicator bar that I am drawing is appearing outside of the bounds of the ListView (in my case above) when the ListViewItem I am over is at the top and only partially visible.

The trigger to draw the bar is to take whatever ListViewItem I am currently hovering over, grab it's top bounds value and draw the line. When the ListViewItem is not fully visible yet, the bar is draw above the ListView itself and obviously looks weird.

Summary: Trying to determine if a given ListViewItem is only Partially visible in a ListView.

In this case, code is not really all that relevant but I can post some if you like.

Thanks


Solution

  • You can probably use a combination of ScrollViewer.VerticalOffset or ScrollViewer.ViewPortHeight and the listviewitem's location with respect to the listview itself

    itemPosition = ListViewItem.TransformToAncestor(ListView).Transform(new Point(0, 0));
    

    To tell if it is fully visible you should just need to do the following

    itemPosition.Y + itemHeight - viewPortHeight
    

    Then if that value is between the itemHeight or -viewPortHeight, then it is visible

    OR, what is probably a little cleaner:

    (-itemPosition.Y < itemHeight && itemPosition.Y < viewPortHeight)