views:

198

answers:

1

I have a a bunch of user controls(about 15, one for each record) in a stackpanel that extends way down my page. I put this in a scrollviewer so that the user wouldn't have to scroll the browser but instead can just scroll the scrollviewer panel.

ANyone know how to programatically scroll to a specific user control in my stack panel. I want a dropdown at the top toi represetn each record so that when selected the scroll viewer scrolls to that user control.

ANy thoughts? I see that the scrollviewer has a ScrollToVerticalOffset() method but I don't know how I would calculate the offset of these controls in the stackpanel.

Thanks!

A: 

I imagine something like the following would work:

void ScrollToUserControl(UserControl uc)
{
    double amountToScroll = 0;
    for (int i = 0; i < stackPanel.Children.Count; i++)
    {
        if (stackPanel.Children[i] == uc)
            break;

        amountToScroll += stackPanel.Children[i].ActualHeight;

    }

    scrollViewer.UpdateLayout();
    scrollViewer.ScrollToVerticalOffset(amountToScroll);
}
David
This worked perfectly! Many Thanks!
Eric