scrollviewer current position
-
Thursday, August 02, 2012 5:14 PM
Hi,
I am using scrollviwer. inside it i place a stackpanel and some controls are added to this stackpanel from codebehind. so after adding all the contents, when i checked in code, i found the actual height of stackpanel is 1200. so the scrolling height of scrollviwer will be 1200? so when i start the scrolling, i need to find the current position of scrollviewer. Say the value is 300 at some point, 700 at some other point and 1200 and the end of scrolling ( to the bottom of page). how can i check this in code? will it affect the maximise/minimise state of window?
All Replies
-
Thursday, August 02, 2012 7:40 PMModerator
Hiya, there are a load of special properties in there, for you to determine and move the scrollviewer from code.
Check out the following:
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.extentheight.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.extentwidth.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.scrollablewidth.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.scrollableheight.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.panningratio.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.horizontaloffset.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.verticaloffset
I show an examination of the ScrollViewer and detecting/calculating/manipulating some of these properties in the following TechNet sample:
http://code.msdn.microsoft.com/SubtleScrollViewer-No-d31d3248
That should get you started, or at l;east give you the terms to search better with :)
#PEJL
- Edited by XAML guyMicrosoft Community Contributor, Moderator Thursday, August 02, 2012 7:40 PM
- Marked As Answer by Ajin Prasad Monday, August 06, 2012 4:43 PM
-
Friday, August 03, 2012 10:04 AM
-
Saturday, August 04, 2012 8:35 AM
ScrollViewer Height and Width is affected by window resizing.
To get the values related to scrollviewer Position you can use ScrollViewer_ScrollChanged Event. The event is triggered whenever you scroll the ScrollViewer.
Here is a code snippet!!!
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { Console.WriteLine("Horizontal Position: " + e.HorizontalOffset); /* Gets the current Horizontal Position of the scrollviewer*/ Console.WriteLine("Change in Horizontal Position: " + e.HorizontalChange); /* Gets the change in Horizontal Position */ Console.WriteLine("Vertical Position: " + e.VerticalOffset); /* Gets the current Vertical Position of the scrollviewer*/ Console.WriteLine("Change in Vertical Position : " + e.VerticalChange); /* Gets the change in Vertical Position */ Console.WriteLine("Height of the ScrollViewer : " + e.ViewportHeight); /*Gets the Total Height of the ScrollViewer*/ Console.WriteLine("Width of the ScrollViewer: " + e.ViewportWidth); /*Gets the Total Width of the ScrollViewer*/ }Output of the Code:
Horizontal Position: 0
Change in Horizontal Position: 0
Vertical Position: 145.650541516245
Change in Vertical Position : 9.24765342960288
Height of the ScrollViewer: 311
Width of the ScrollViewer: 492
Happy Programming!!!
- Edited by Srithar Saturday, August 04, 2012 8:44 AM
- Marked As Answer by Ajin Prasad Monday, August 06, 2012 4:43 PM
-
Saturday, August 04, 2012 12:26 PM
Hello Ajin.
If your ScrollViewer had a set height of 100 and the content had a height of 1200, your ScrollableHeight would be 1100. (Content Height less ScrollViewer ActualHeight)
Knowing that you could always just bind to the ScrollViewer.ContentVerticalOffset or call it from code-behind.
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { double a = scrollViewer.ContentVerticalOffset; MessageBox.Show(a.ToString()); }And the binding in XAML...
<Grid x:Name="LayoutRoot"> <ScrollViewer x:Name="scrollViewer" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100"> <StackPanel Height="1200" Width="100"> <StackPanel.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="White" Offset="0.9"/> <GradientStop Color="Red" Offset="1"/> </LinearGradientBrush> </StackPanel.Background> </StackPanel> </ScrollViewer> <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ScrollableHeight, ElementName=scrollViewer}" VerticalAlignment="Top" Margin="104,0,0,0"/> <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ContentVerticalOffset, ElementName=scrollViewer, Mode=OneWay}" VerticalAlignment="Top" Margin="104,19.96,0,0"/> <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="104,78.04,0,0" Click="Button_Click"/> </Grid>
~Christine
-
Friday, December 07, 2012 6:13 AMhi i am unable to get scroll_Changed Event in SilverLight please suggest me..

