Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BringIntoView Method

In WPF, I have a scrollviewer with 7 different groupboxes in it. Each groupbox is it's own individual section for different forms. Because the scrollviewer is so long, I've placed a series of buttons in a stackpanel on the left side of the panel, and done code-behind so that clicking a button triggers the BringIntoView method for the corresponding groupbox.

My question is, is there any way to force BringIntoView to position the groupbox at the top of the visible section each time. By default BringIntoView only moves the scrollviewer until the entire groupbox is within view, not so that it's the topmost item displayed. Is there a way around this?

I looked into using ScrollToVerticalOffset, but couldn't figure out how to get the right position, mostly because it's saying that groupbox has no Locate property, which I was going to use to get the position, then set it with ScrollToVerticalOffset. One thing I thought of while typing this was to have each button-press jusmp to the top groupbox first, then immediately continue to the correct one, but this seems kind of hack-ish, and I prefer to find the correct way. :)

like image 917
Keven M Avatar asked Dec 11 '11 21:12

Keven M


1 Answers

Try this one:

private void ScrollToGroupBox(GroupBox groupBox)
{
  GeneralTransform groupBoxTransform = groupBox.TransformToAncestor(scrollViewer);
  Rect rectangle = groupBoxTransform.TransformBounds(new Rect(new Point(0, 0), groupBox.RenderSize));
  scrollViewer.ScrollToVerticalOffset(rectangle.Top + scrollViewer.VerticalOffset);
}

The code gets the position of your GroupBox inside the Scrollviewer and scrolls to it.

like image 77
SvenG Avatar answered Nov 05 '22 07:11

SvenG