1

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

Isildur
  • 40
  • 6
  • Welcome to Staock Overflow. Your question is well written, but you can improve things by adding tags that describe the languages relevant to the question – GreenAsJade Jan 29 '14 at 08:28
  • Hopefully someone will be along who can actually answer the question. I'm not a vb.net person, but generally I would expect the answer to this question to entail asking the scroller how far it is scrolled. Maybe that is something to look into... – GreenAsJade Jan 29 '14 at 09:08
  • This was my first idea, but I got no Idea how to access the built in ScrollViewer of the ListView. I do not want to wrap an extra scrollviewer around the control. Solutions in vb.net or C# appreciated. – Isildur Jan 29 '14 at 09:26
  • http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp – GreenAsJade Jan 29 '14 at 09:46
  • Thanks, the link helped. By calling Media.VisualTreeHelper.GetChild(Mylistview,0).Child I got the Scrollviewer and apparently Scrollviewer.VerticalOffset gives me the amount of Items Scrolled down and that is exactly what I needed. – Isildur Jan 29 '14 at 12:58
  • Yay. I summarised our finding in an answer - if you would be kind enough to accept (or critique) that would be great. – GreenAsJade Jan 29 '14 at 13:13

1 Answers1

0

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.

Isildur
  • 40
  • 6
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98