How can I make the Silverlight ScrollViewer scroll to show a child control with focus?

后端 未结 4 2022
时光取名叫无心
时光取名叫无心 2021-02-05 16:30

I have a ScrollViewer which contains a Grid with multiple controls in it. The user can tab through the controls, but eventually they tab to a control that isn\'t in view - so th

4条回答
  •  佛祖请我去吃肉
    2021-02-05 16:56

    I tested this using Silverlight 3. I am not sure about SL2.

    This is my XAML:

    
        
            

    And this is the code-behind:

    private void ScrollViewer_KeyUp(object sender, KeyEventArgs e)
    {
        ScrollViewer scrollViewer = sender as ScrollViewer;
        FrameworkElement focusedElement = FocusManager.GetFocusedElement() as FrameworkElement;
        GeneralTransform focusedVisualTransform = focusedElement.TransformToVisual(scrollViewer);
        Rect rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
        double newOffset = scrollViewer.VerticalOffset + (rectangle.Bottom - scrollViewer.ViewportHeight);
        scrollViewer.ScrollToVerticalOffset(newOffset);
    }
    

    What I did was to click on Button #1 and tab until I get to Button #20. It worked for me. Give it a try and let me know how it works for you.

提交回复
热议问题