简体   繁体   中英

How do I ensure the entire parent control is visible when a compound XAML control is Tab Selected?

I have a compound XAML control. It looks like this:

<UI:CompoundControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Dynamics="clr-namespace:MyNamespace.UI" Height="Auto" >
    <Stackpanel Orietnation="Vertical">
        <TextArea x:Name="lblTop" DockPanel.Dock="Left">Label</TextArea >
        <TextArea x:Name="lblBottom" DockPanel.Dock="Left">Label</TextArea >
    </Stackpanel >
</UI:CompoundControl>

When the application runs, there is a UIElementCollection of these CompoundControl s, which displays them in a scrollable list.

When I press the Tab key, the application moves focus to the next TextArea in sequence, scrolling the view to do so. However, it only scrolls to show the focused control, so I'm running into an issue where pressing Tab doesn't show the entirety of the CompoundControl , only the upper half.

Is there some attribute I can set so that the automatic scrolling shows the entire control, instead of simply the upper half?

Figured out a way to make it work by responding to the top control's "GotFocus" event. scrollViewer in this case is the parent ScrollViewer that the compound control gets added to.

private const int SCROLL_PADDING = 10;
private void lblTop_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            if (scrollViewer == null) return;
            FrameworkElement element = e.OriginalSource as FrameworkElement;
            var transform = element.TransformToVisual(scrollViewer);
            var positionInScrollViewer = transform.Transform(new Point(0, 0));

            //Scrolls the viewer down far enough to see this control + the control directly below it.
            if (positionInScrollViewer.Y < 0 ||
                positionInScrollViewer.Y + SCROLL_PADDING > scrollViewer.ViewportHeight)
            {
               scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset +
                    positionInScrollViewer.Y - SCROLL_PADDING);
            }


        }

This will scroll the ScrollViewer past the entirety of the compound control, but only if the top part of the compound control is off, or nearly-off, the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM