如何使 Silverlight ScrollViewer 滚动以显示具有焦点的子控件? [英] How can I make the Silverlight ScrollViewer scroll to show a child control with focus?

查看:57
本文介绍了如何使 Silverlight ScrollViewer 滚动以显示具有焦点的子控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ScrollViewer,它包含一个带有多个控件的 Grid.用户可以通过 Tab 键浏览控件,但最终他们会切换到不在视图中的控件 - 因此他们必须手动滚动以使控件再次可见.

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 they have to manully scroll to make the control visible again.

有什么办法可以让 ScrollViewer 自动滚动,让焦点控件始终可见.如果做不到这一点,除了在每个控件上收听 GotFocus 事件然后滚动 ScrollViewer 以使控件可见之外,还有什么方法可以使这项工作正常进行吗?

Is there any way to make the ScrollViewer scroll automatically so that the focussed control is always visible. Failing that, is there any way I can make this work, short of listening to a GotFocus event on every control and then scrolling the ScrollViewer to make the control visible?

目前我使用的是 Silverlight 2.

At present I'm using Silverlight 2.

推荐答案

我使用 Silverlight 3 对此进行了测试.我不确定 SL2.

这是我的 XAML:

<ScrollViewer Height="200" Width="200" KeyUp="ScrollViewer_KeyUp">
    <StackPanel>
        <Button Content="1" Height="20" />
        <Button Content="2" Height="20" />
        <Button Content="3" Height="20" />
        <Button Content="4" Height="20" />
        <Button Content="5" Height="20" />
        <Button Content="6" Height="20" />
        <Button Content="7" Height="20" />
        <Button Content="8" Height="20" />
        <Button Content="9" Height="20" />
    <Button Content="10" Height="20" />
        <Button Content="11" Height="20" />
        <Button Content="12" Height="20" />
        <Button Content="13" Height="20" />
        <Button Content="14" Height="20" />
        <Button Content="15" Height="20" />
        <Button Content="16" Height="20" />
        <Button Content="17" Height="20" />
        <Button Content="18" Height="20" />
        <Button Content="19" Height="20" />
        <Button Content="20" Height="20" />
    </StackPanel>
</ScrollViewer>

这是代码隐藏:

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);
}

我所做的是单击按钮 #1 和选项卡,直到到达按钮 #20.它对我有用.试一试,让我知道它对你有什么作用.

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.

这篇关于如何使 Silverlight ScrollViewer 滚动以显示具有焦点的子控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆