2012-04-30 15 views
0

私はその周りのスクロールビューア内にTextblockを持っています。私のアプリケーションは完全にリモートで制御されるので、このコンテキストではナビゲーションはキーの上下左右で実行されます。ScrollViewerのキーボードナビゲーション

私はTextblockにナビゲートできますが、そこにトラップされます。私はKeyboardNavigation.DirectionalNavigationを配置しようとしました=すべてのコントロールで "続ける"ことができますが、喜びはありません。

次に、スクロールバーまたはスクロールバーを拡張するカスタムコントロールを作成することを考えました。 スクロールバーを拡張する場合は、以下のように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のスクロールバーを変更するかどうかはわかりませんで、またはいずれかのキーが押されたとき、上記のコードでも発火する場合でも。私はテキストブロックを想定し、scrollviewerはイベントを見るコントロールになります。

上記のコードと似たような方法がありますか?Scrollviewersコードの背後にありますか?

+0

あなたがコントロールしている場合は、テキストブロックがフォーカスを取得するテキストブロックの下にあり、アップキーを押してください(コントロールAそれを呼びましょう)。上または下を押すとテキストが上下にスクロールしますが、キーボードでテキストブロックからフォーカスを移動することは決してできません(IEはスクロールビューアが一番下にあっても押し下げてもコントロールAに戻りません)テキストボックスなどのオーバーコントロールでも同じ問題がありましたが、上記と同様のコードを使用して回避します – Oli

答えて

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; 
      } 
     } 
    } 
} 
関連する問題