I hope this isn't a duplicate but I can't find any documentation or examples on how to actually use ScrollToVerticalOffset()
. I'm using it in a Windows Phone 8 app, but I think it will still apply to WP7 and Silverlight (although, feel free to correct me if I'm wrong).
So here is my basic set up (pseudo-code from memory):
<phone.PivotItem>
<ScrollViewer>
<Grid Height="1500">
<Grid.RowDefinitions>
<!-- about 20 rows, all auto-height -->
</Grid.RowDefinitions>
<Border Grid.Row="0">
<TextBox x:Name="txt1" />
</Border>
<Border Grid.Row="1">
<TextBox x:Name="txt2" />
</Border>
<!-- ...... -->
<Border Grid.Row="19">
<TextBox x:Name="txt20" />
</Border>
</Grid>
</ScrollViewer>
</phone.PivotItem>
So as you can see, I've got a ScrollViewer
within a PivotItem
, and inside is a Grid
. In the Grid
there are about 20 TextBox
s, each within a Border
. I am dynamically setting focus to one of these TextBox
s when this page loads, so anytime I set focus to TextBox
#6-20 (roughly) - I have to manually scroll down to see it. I want to auto-scroll my ScrollViewer
so that whichever TextBox
has focus, it will be centered for the user to see.
The documentation for ScrollToVerticalOffset()
says:
Scrolls the content that is within the ScrollViewer to the specified
vertical offset position.
And that it accepts a type of System.Double
.
What I don't understand is A) the value I'm supposed to pass, and B) how I could even get that value? Is it supposed to be a number between 0 and the height of my Grid
(1500)? If so, how could I determine the position of any given TextBox
so I can scroll to it?
If there are any straightforward examples, please feel free to link out to them. I'm not sure if the content within the ScrollViewer
matters when calling this method, but in case it does I wanted to show exactly how I'm using it.
Many thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…