Question

I would like to get the 'real' Y-value of the point i clicked on when scrolled down a listview.

Here is the XAML:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MaxHeight="350" MinHeight="350" Width="525">
<Grid>
    <ListView Name="MyListView" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
    </ListView>
</Grid>
</Window>

and here the code:

Class MainWindow 
Sub New()
    InitializeComponent()
    For i As Integer = 0 To 100
        MyListView.Items.Add(i)
    Next
End Sub
Private Sub MyListView_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs) Handles MyListView.MouseLeftButtonUp
    MsgBox(e.GetPosition(MyListView).Y)
End Sub
End Class

My problem now is that no matter how far down I scrolled, the top is always 1 and the bottom is always 309, but when scrolled down i would like to get values higher than 309. Is there any way to find out the 'real' Y-coordinate I clicked on or maybe find out how far down I scrolled and then being able to calculate the adjusted value? Wrapping the listview in a scrollviewer control is not an option btw.

Thanks for any answers

Was it helpful?

Solution

In order to determine the real Y value, you need to know the amount that has been scrolled, which the scrollviewer can tell you.

You can obtain the scrollviewer as a child of the listview (Media.VisualTreeHelper.GetChild(Mylistview,0).Child) , and from there the vertical offset it currently has (Scrollviewer.VerticalOffset). The vertical Offset does not give you the the y-coordinate-offset, instead you get the amount of listview-items scrolled down. You can easily work with that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top