上下文菜单中的文本框失去了鼠标移动的焦点

发布于 2024-12-14 03:18:48 字数 190 浏览 5 评论 0原文

我有一个带有 MenuItem 的 ControlTemplate 的文本框,它位于 ContextMenu 内。文本框运行良好,我可以正确输入内容。但是,如果我将鼠标移动到上下文菜单中的任何其他菜单项上,它们就会获得焦点,而我会失去文本框的焦点。此时,我必须单击返回文本框才能继续输入。

是否有解决此问题的模式或可接受的方法?

谢谢

I have a Textbox withing the ControlTemplate of a MenuItem, which is inside a ContextMenu. The Textbox works well and I can type in it properly. But if I move the mouse over any of the other menu items in the context menu, they claim focus and I lose focus from the textbox. At this point I have to click back into the textbox to continue typing.

Is there a pattern or accepted method of resolving this issue?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-12-21 03:18:48

如果您想收回焦点,可以输入如下内容。

textBox.CaptureMouse();
textBox.ReleaseMouseCapture();

我认为它可以将焦点集中到文本框。

If you want to take back focus, you can type as below.

textBox.CaptureMouse();
textBox.ReleaseMouseCapture();

I think it can catch focus to textbox.

辞取 2024-12-21 03:18:48

在尝试了一些不同的事情之后,我得到了一些工作:

对于可以捕获焦点(在鼠标输入时)的所有其他菜单项,为 PriviewGoTKeyboardFocus 事件设置 e.Handled = true :

void menuItem_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
  e.Handled = true;
}

可以从窗口基类自动执行此操作通过循环浏览上下文菜单中的所有菜单项。这需要劫持插入文本框的那些菜单项的标签。

void contextMenu_Opened(object sender, RoutedEventArgs e)

{
  ContextMenu contextMenu = sender as ContextMenu;
  foreach (FrameworkElement frameworkElement in contextMenu.Items)
  {
    if (frameworkElement is MenuItem)
    {
      MenuItem menuItem = (frameworkElement as MenuItem);
      if (!(menuItem.Tag != null && menuItem.Tag.ToString() == "MaintainFocus"))
        menuItem.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler(menuItem_PreviewGotKeyboardFocus);
    }
  }
}
void contextMenu_Closed(object sender, RoutedEventArgs e)
{
  ContextMenu contextMenu = sender as ContextMenu;
  foreach (FrameworkElement frameworkElement in contextMenu.Items)
  {
    if (frameworkElement is MenuItem)
    {
      MenuItem menuItem = (frameworkElement as MenuItem);
      if (!(menuItem.Tag != null && menuItem.Tag.ToString() == "MaintainFocus"))
        menuItem.PreviewGotKeyboardFocus -= menuItem_PreviewGotKeyboardFocus;
    }
  }
}

Well after trying a few different things, I got something to work:

For all other menu items that can capture focus (on mouse enter), set e.Handled = true for the PriviewGoTKeyboardFocus event:

void menuItem_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
  e.Handled = true;
}

One can do this automatically from a window base class by looping through all menu items in a context menu. This requires hijacking the tag for those menuitems in which you insert the textbox.

void contextMenu_Opened(object sender, RoutedEventArgs e)

{
  ContextMenu contextMenu = sender as ContextMenu;
  foreach (FrameworkElement frameworkElement in contextMenu.Items)
  {
    if (frameworkElement is MenuItem)
    {
      MenuItem menuItem = (frameworkElement as MenuItem);
      if (!(menuItem.Tag != null && menuItem.Tag.ToString() == "MaintainFocus"))
        menuItem.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler(menuItem_PreviewGotKeyboardFocus);
    }
  }
}
void contextMenu_Closed(object sender, RoutedEventArgs e)
{
  ContextMenu contextMenu = sender as ContextMenu;
  foreach (FrameworkElement frameworkElement in contextMenu.Items)
  {
    if (frameworkElement is MenuItem)
    {
      MenuItem menuItem = (frameworkElement as MenuItem);
      if (!(menuItem.Tag != null && menuItem.Tag.ToString() == "MaintainFocus"))
        menuItem.PreviewGotKeyboardFocus -= menuItem_PreviewGotKeyboardFocus;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文