<small id='7Azib'></small><noframes id='7Azib'>

      <bdo id='7Azib'></bdo><ul id='7Azib'></ul>

    <i id='7Azib'><tr id='7Azib'><dt id='7Azib'><q id='7Azib'><span id='7Azib'><b id='7Azib'><form id='7Azib'><ins id='7Azib'></ins><ul id='7Azib'></ul><sub id='7Azib'></sub></form><legend id='7Azib'></legend><bdo id='7Azib'><pre id='7Azib'><center id='7Azib'></center></pre></bdo></b><th id='7Azib'></th></span></q></dt></tr></i><div id='7Azib'><tfoot id='7Azib'></tfoot><dl id='7Azib'><fieldset id='7Azib'></fieldset></dl></div>

      <tfoot id='7Azib'></tfoot>

      1. <legend id='7Azib'><style id='7Azib'><dir id='7Azib'><q id='7Azib'></q></dir></style></legend>

        WPF FocusNavigationDirection、MoveFocus 和箭头键

        WPF FocusNavigationDirection, MoveFocus and Arrow keys(WPF FocusNavigationDirection、MoveFocus 和箭头键)
        1. <legend id='CGNVG'><style id='CGNVG'><dir id='CGNVG'><q id='CGNVG'></q></dir></style></legend>
          <tfoot id='CGNVG'></tfoot>

              <tbody id='CGNVG'></tbody>
                <bdo id='CGNVG'></bdo><ul id='CGNVG'></ul>
              • <i id='CGNVG'><tr id='CGNVG'><dt id='CGNVG'><q id='CGNVG'><span id='CGNVG'><b id='CGNVG'><form id='CGNVG'><ins id='CGNVG'></ins><ul id='CGNVG'></ul><sub id='CGNVG'></sub></form><legend id='CGNVG'></legend><bdo id='CGNVG'><pre id='CGNVG'><center id='CGNVG'></center></pre></bdo></b><th id='CGNVG'></th></span></q></dt></tr></i><div id='CGNVG'><tfoot id='CGNVG'></tfoot><dl id='CGNVG'><fieldset id='CGNVG'></fieldset></dl></div>

              • <small id='CGNVG'></small><noframes id='CGNVG'>

                  本文介绍了WPF FocusNavigationDirection、MoveFocus 和箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的应用程序(一个带有 6 个按钮的网格 - 2 行 3 个 - 用于测试)并且正在按如下方式处理左右箭头键

                  I have a simple application (a grid with 6 buttons - 2 rows of 3 - on it for testing) and am handling left and right arrow keys as follows

                      private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
                      {
                          FocusNavigationDirection focusDirection = new System.Windows.Input.FocusNavigationDirection();
                  
                          switch (e.Key)
                          {
                              case Key.Left:
                                  focusDirection = System.Windows.Input.FocusNavigationDirection.Left;
                                  break;
                              case Key.Right:
                                  focusDirection = System.Windows.Input.FocusNavigationDirection.Right;
                                  break;
                              default:
                                  break;
                          }
                          TraversalRequest request = new TraversalRequest(focusDirection);
                  
                          // Gets the element with keyboard focus.
                          UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                  
                          // Change keyboard focus.
                          if (elementWithFocus != null)
                          {
                              elementWithFocus.MoveFocus(request);
                          }
                  
                      }
                  

                  不幸的是,这并不像我预期的那样,因为焦点似乎总是朝着与 FocusNavigationDirection 指定的方向相反的方向移动.

                  Unfortunately, this doesn't behave as I expect as the focus always seems to move in the opposite direction to that specified by the FocusNavigationDirection.

                  对为什么会这样有什么想法吗?MSDN Documentation 有点含糊左侧"是如何定义的.

                  Any thoughts on why this would be? The MSDN Documentation is a bit vague on how "to the left of" is defined.

                  如果需要,我还将每个按钮的制表位定义为 1 到 6.

                  In case it is needed, I have also defined the tab stops of each of the buttons as 1 through 6.

                  推荐答案

                  为什么要获取 elementWithFocus 而可以使用this"(window own scope)?

                  Why do you need to get the elementWithFocus while you can use "this" (window own scope)?

                  我稍微修改了你的代码,它对我有用:

                  I modified your code a bit and it worked for me:

                      private void Window_OnPreviewKeyDown(object sender, KeyEventArgs e)
                      {
                          switch (e.Key)
                          {
                              case Key.Left:
                                  this.MoveFocus(FocusNavigationDirection.Previous);
                                  break;
                              case Key.Right:
                                  this.MoveFocus(FocusNavigationDirection.Next);
                                  break;
                              default:
                                  break;
                          }
                      }
                  
                      private void MoveFocus(FocusNavigationDirection direction)
                      {
                          var request = new TraversalRequest(direction);
                          this.MoveFocus(request);
                      }
                  

                  这篇关于WPF FocusNavigationDirection、MoveFocus 和箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                    1. <i id='JgW4C'><tr id='JgW4C'><dt id='JgW4C'><q id='JgW4C'><span id='JgW4C'><b id='JgW4C'><form id='JgW4C'><ins id='JgW4C'></ins><ul id='JgW4C'></ul><sub id='JgW4C'></sub></form><legend id='JgW4C'></legend><bdo id='JgW4C'><pre id='JgW4C'><center id='JgW4C'></center></pre></bdo></b><th id='JgW4C'></th></span></q></dt></tr></i><div id='JgW4C'><tfoot id='JgW4C'></tfoot><dl id='JgW4C'><fieldset id='JgW4C'></fieldset></dl></div>
                          <bdo id='JgW4C'></bdo><ul id='JgW4C'></ul>

                          <small id='JgW4C'></small><noframes id='JgW4C'>

                          <tfoot id='JgW4C'></tfoot>
                              <tbody id='JgW4C'></tbody>
                          • <legend id='JgW4C'><style id='JgW4C'><dir id='JgW4C'><q id='JgW4C'></q></dir></style></legend>