문제

Here is what I am trying to do. I have a Scrollviewer and nested inside it are UserControls, nested in other UserControls, inside grids, stackpanels and other containers. When I click a button that adds yet another child somewhere within this hierarchy I would like to scroll to see said child. Code of function below:

 public static void ScrollParentNamedScrollViewerDown(DependencyObject child, string strTargetParent, DependencyObject newStartPoint = null)            
    {
        if(child == null) return;
        if(newStartPoint == null) newStartPoint = child;
        ScrollViewer scvPotentialTarget = GetParentOfType<ScrollViewer>(newStartPoint);
        if (scvPotentialTarget == null) return;
        if (scvPotentialTarget.Name != strTargetParent)
        {
            ScrollParentNamedScrollViewerDown(child, strTargetParent, scvPotentialTarget);
        }
        else
        {
            UIElement scrollTarget = child as UIElement;
            if (scrollTarget == null)
                scvPotentialTarget.ScrollToBottom();
            else{
                Point pTarget = scrollTarget.TranslatePoint(new Point(0, 0), scvPotentialTarget);                    
                if (pTarget == null)
                    scvPotentialTarget.ScrollToBottom();
                else
                    scvPotentialTarget.ScrollToVerticalOffset(pTarget.Y);
            }
        }

This function is used like this:

gbSubWindow.Visibility = System.Windows.Visibility.Visible;
gbSubWindow.Content = uc;
ScrollParentNamedScrollViewerDown(gbSubWindow, "OmsWindowScrollViewer");

where gbSubWindow was a previously empty, hidden expander at the bottom of a nested UserControl

For some reason I am getting ridiculously low values for the vertical offset when I execute the TranslatePoint function - my scrollviewer scrollheight is near 800, and I am adding a child that will show up at the very bottom beneath 2 large child control and I am getting values in 160ish range.

Anyone have any ideas, what is happening here?

도움이 되었습니까?

해결책

In case that is the answer

BringIntoView on the control
Since UserControl derives from FrameWorkElelement it should work.
I have only used it for ListItem

FrameworkElement.BringIntoView Method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top