<legend id='Dq3Yt'><style id='Dq3Yt'><dir id='Dq3Yt'><q id='Dq3Yt'></q></dir></style></legend>

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

      <tfoot id='Dq3Yt'></tfoot>
        <bdo id='Dq3Yt'></bdo><ul id='Dq3Yt'></ul>

      1. <small id='Dq3Yt'></small><noframes id='Dq3Yt'>

        WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown()

        时间:2023-06-10

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

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

                <tbody id='sUS94'></tbody>
                <tfoot id='sUS94'></tfoot>
                  <bdo id='sUS94'></bdo><ul id='sUS94'></ul>
                • 本文介绍了WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 WPF 应用程序中,我有一个从 TextBox 派生的控件,如下所示:

                  In a WPF application, I have a control that I have derived from TextBox like this:

                  public class SelectableTextBlock : TextBox
                  {
                      protected override void OnKeyDown(KeyEventArgs e)
                      {
                          base.OnKeyDown(e);
                          e.Handled = false;
                      }
                  }
                  

                  在 TextBox 中输入空格或按 Backspace 时不会调用 OnKeyDown 方法,但会触发其他输入,包括正常的可打印字符(例如 'a')和修饰键(例如 ).

                  The OnKeyDown method is not called when entering a space into the TextBox, nor when hitting Backspace, but does fire for other input including normal printable characters (e.g. 'a') and modifier keys (e.g. ).

                  我正在使用这个控件,并将 IsReadOnly 设置为 true,这样我就可以显示可选择的、不可编辑的文本.WPFToolkit 的 DataGrid 中使用的控件,我希望 KeyDown 事件沿数据网格向上传播,即使 SelectableTextBlock 具有焦点,这就是我使用自定义控件将事件显式标记为未处理的原因.

                  I'm using this control with IsReadOnly set to true so I can display selectable, uneditable text. The control used within WPFToolkit's DataGrid, and I want KeyDown events to propagate up the data grid, even if the SelectableTextBlock has focus, which is why I am using a custom control to explicitly mark the event as unhandled.

                  问题是事件甚至没有让我控制某些键.我不能只使用 DataGrid 中的 OnPreviewKeyDown 来解决这个问题,因为我确实希望数据网格中使用的其他控件能够吞下空间 KeyDown 事件.

                  The problem is that the event isn't even making it to my control for certain keys. I can't just use OnPreviewKeyDown in the DataGrid to get round this, since I do want other controls used in the data grid to swallow the space KeyDown event.

                  有谁知道我怎样才能让空格键向上传播的 KeyDown 事件?

                  Does anyone know how I can get the KeyDown event for the space key to propagate up?

                  谢谢.

                  推荐答案

                  似乎问题在于空格(和退格等)按键事件已经在 TextBox 中处理,然后它会冒泡到我的派生控件.正如 Wim 所发布的,我认为这是文本撰写过程的一部分.

                  It seems the problem is that the space (and backspace etc.) key down event is being handled already within the TextBox, before it bubbles up to my derived control. I assume as part of the text composition process, as Wim posted.

                  为了解决这个问题,我添加了一个处理程序,即使它已经被处理,它也会接收按键按下事件,并将其 Handled 成员设置为 false,以允许它继续正常冒泡.在下面的示例中,它仅针对空格键执行此操作,但在我的情况下,我需要对我 真正 不想在我的 SelectedableTextBlock 中处理的任何键事件执行此操作,因为我还不知道父母可能对哪些关键事件感兴趣.

                  To workaround this, I've added a handler that will receive the key down event even it has already been handled, and sets its Handled member to false, to allow it to carry on bubbling up normally. In the example below it just does this for space keys, but in my case I'll need to make it do this for any key events that I really don't want handled in my SelectedableTextBlock, as I don't know what key events parents might be interested in yet.

                  public class SelectableTextBlock : TextBox
                  {
                      public SelectableTextBlock() : base()
                      {
                          this.AddHandler(SelectableTextBlock.KeyDownEvent, new RoutedEventHandler(HandleHandledKeyDown), true);
                      }
                  
                      public void HandleHandledKeyDown(object sender, RoutedEventArgs e)
                      {
                          KeyEventArgs ke = e as KeyEventArgs;
                          if (ke.Key == Key.Space)
                          {
                              ke.Handled = false;
                          }
                      }
                      ...
                  }
                  

                  如果有人有更好的解决方案,我当然仍然感兴趣......

                  I am of course still interested if anyone has a better solution...

                  谢谢,E.

                  这篇关于WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何禁用文本框中的光标? 下一篇:C# WinForms - 智能文本框控件根据文本框宽度自动格式化路径长度

                  相关文章

                • <tfoot id='Fj9E2'></tfoot>

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

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

                    2. <small id='Fj9E2'></small><noframes id='Fj9E2'>