views:

784

answers:

2

I have a scrollviewer with dynamic content. On an event, a new content is made visible and the scrollbar appears. How do i make it auto scroll to have that content in view?

Thanks, Shawn Mclean

+1  A: 

Use ScrollToVerticalOffset() to do this, passing in the coordinates of the new content.

var newContent = GetNewContent();

var generalTransform = newContent.TransformToVisual(
    Application.Current.RootVisual as UIElement);

Point offset = generalTransform.Transform(new Point(0, 0));
double controlTop = offset.Y;
double controlLeft = offset.X;

scrollViewer.ScrollToVerticalOffSet(controlTop);
scrollViewer.ScrollToHorizontalOffSet(controlLeft);
Sander Rijken
render size does not give position.
Shawn Mclean
I fixed the code, thanks
Sander Rijken
A: 

Are you sure Scrollviewer is the control you need here?

Sounds to me like you ought to be using ListBox (which you can heavily style if necessary). It has a ScrollIntoView(item) method which would acheive your goal.

AnthonyWJones