2012-04-30 54 views
0

我在它周围的滚动查看器中有一个Textblock。我的应用程序完全由远程控制,所以在这种情况下,导航使用键上,下,左和右进行。ScrollViewer中的键盘导航TextBlock

我可以导航到文本块,但然后我被困在那里。我已经尝试在每个控件中放置KeyboardNavigation.DirectionalNavigation =“Continue”,但我无法享受。

然后我想到做一个自定义控件来扩展滚动条或scrollviewer。 如果扩展滚动条,我可以按照以下方式覆盖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代码后面?

+0

如果您处于文本块下的控件(称为控件A),并按下向上键,则文本块将获得焦点。现在向上或向下按可以上下滚动文本,但是永远不能使用键盘将焦点从文本块移开(即使滚动查看器位于最下方,IE也会按下,不会使您返回到控制A)对于诸如文本框之类的控件,我们遇到了同样的问题,但是使用类似于上面的代码来解决它 – Oli

回答

1

我最终通过创建自定义控件来解决这个问题。如果滚动查看器可以按键被按下的方向滚动,我可以确定。如果是,则事件传递到底层的scrollviewer。如果不是该事件被标记为已处理并且聚焦按下按键的方向移动。

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