0

我在它周围的滚动查看器中有一个文本块。我的应用程序完全由遥控器控制,因此在这种情况下,导航是通过向上、向下、向左和向右键执行的。

我可以导航到文本块,但后来我被困在那里。我已经尝试将 KeyboardNavigation.DirectionalNavigation="Continue" 放在每个控件中,但没有任何乐趣。

然后我想制作一个扩展滚动条或滚动查看器的自定义控件。如果扩展滚动条,我可以按以下方式覆盖 keydown。

protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (this.Orientation == Orientation.Vertical)
        {
            if (e.Key == Key.Up)
            {
                if (this.Value == this.Minimum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Down)
            {
                if (this.Value == this.Maximum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Left)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                e.Handled = true;
            }
            if (e.Key == Key.Right)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                e.Handled = true;
            }
        }

        if (this.Orientation == Orientation.Horizontal)
        {
            if (e.Key == Key.Left)
            {
                if (this.Value == this.Minimum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Right)
            {
                if (this.Value == this.Maximum)
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                    e.Handled = true;
                }
            }
            if (e.Key == Key.Up)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                e.Handled = true;
            }
            if (e.Key == Key.Down)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                e.Handled = true;
            }
        }
        base.OnPreviewKeyDown(e);
    }
}

问题是我不确定如何更改 ScrollViewer 滚动条以使用自定义滚动条,或者即使上面的代码甚至会在按下某个键时触发。我认为文本块和滚动查看器将是查看事件的控件。

有没有办法做类似于上面的代码但在后面的 Scrollviewers 代码中?

4

1 回答 1

1

我最终通过创建自定义控件解决了这个问题。我计算出滚动查看器是否可以在按键被按下的方向上滚动。如果是,则事件传递到底层滚动查看器。如果不是,则将事件标记为已处理,并将焦点移向按键方向。

public class KNScrollViewer : ScrollViewer
{
    static KNScrollViewer()
    {

    }

    private bool canScrollUp
    {
        get
        {
            return this.ScrollableHeight > 0 && this.VerticalOffset > 0;
        }
    }

    private bool canScrollDown
    {
        get
        {
            return this.ScrollableHeight > 0 &&
              this.VerticalOffset + this.ViewportHeight < this.ExtentHeight;
        }
    }

    private bool canScrollLeft
    {
        get
        {
            return this.ScrollableWidth > 0 && this.HorizontalOffset > 0;
        }
    }

    private bool canScrollRight
    {
        get
        {
            return this.ScrollableWidth > 0 &&
            this.HorizontalOffset + this.ViewportWidth < this.ExtentWidth;
        }
    }

    public bool CanScroll
    {
        get
        {
            if (canScrollUp || canScrollDown || canScrollLeft || canScrollRight)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Up)
        {
            if (!canScrollUp)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
                e.Handled = true;
            }
        }
        if (e.Key == Key.Down)
        {
            if (!canScrollDown)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                e.Handled = true;
            }
        }
        if (e.Key == Key.Left)
        {
            if (!canScrollLeft)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
                e.Handled = true;
            }
        }
        if (e.Key == Key.Right)
        {
            if (!canScrollRight)
            {
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
                e.Handled = true;
            }
        }
    }
}
于 2012-05-02T16:47:37.123 回答