WPF TextBox MenuItem在移动鼠标时失去焦点

时间:2021-04-16 00:59:39

In my WPF application I want a menu item to have a text box. I have managed to do this using the following code:

在我的WPF应用程序中,我想要一个菜单​​项有一个文本框。我已设法使用以下代码执行此操作:

<Menu Height="23" HorizontalAlignment="Stretch" Name="MainMenu" VerticalAlignment="Top">
    <MenuItem Header="File">
        <MenuItem Header="Exit" Click="menuItemExit_Click" />
    </MenuItem>
    <MenuItem Header="Settings">
        <MenuItem Header="Some setting" IsCheckable="True" />
        <Separator />
        <MenuItem>
            <MenuItem.Header>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Label Content="Some value:" Margin="0,3,6,0" Padding="0" />
                    <TextBox Margin="0,0,0,6" Grid.Column="1" />
                </Grid>
            </MenuItem.Header>
        </MenuItem>
    </MenuItem>
</Menu>

This code displays the menu item like I expected but if I start typing some value into the text box and then move the mouse (not clicking) away from the text box menu item, the text box loses focus and I cannot continue typing until I click on the text box again. How can I achieve the same behaviour as a text box menu item in WinForms? That is, the text box only loses focus if the user clicks outside the text box or hits the tab key.

此代码显示像我预期的菜单项,但如果我开始在文本框中键入一些值,然后将鼠标(而不是单击)移离文本框菜单项,文本框失去焦点,我无法继续键入,直到我单击再次在文本框上。如何在WinForms中实现与文本框菜单项相同的行为?也就是说,如果用户在文本框外部单击或按Tab键,则文本框仅失去焦点。

Thanks in advance.

提前致谢。

3 个解决方案

#1


6  

As I wrote I am not sure how you use your menu control. But maybe this code snipped can help you or give you a hint:

正如我写的,我不知道你如何使用菜单控件。但也许这段代码可以帮助你或给你一个提示:

<TextBox Margin="0,0,0,6" Grid.Column="1" PreviewLostKeyboardFocus="OnPreviewLostKeyboardFocus"/>

and the according method:

和相应的方法:

private void OnPreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  {
     if (MainMenu.IsKeyboardFocusWithin)
     {
        e.Handled = true;
     }
  }

#2


1  

I know this answer is probably way too late to help you, but maybe it'll help anyone searching for fixes to this issue.

我知道这个答案可能为时已晚,无法帮助您,但也许它可以帮助任何人找到解决此问题的方法。

Setting Focusable=false on the MenuItems also works. It still allows them to be clicked and allows their contained controls to have focus. However, it disables the ability to navigate the menu using just the keyboard, introducing an accessibility issue.

在MenuItems上设置Focusable = false也可以。它仍然允许单击它们并允许其包含的控件具有焦点。但是,它禁用了仅使用键盘导航菜单的功能,从而引入了辅助功能问题。

The accessibility issue can be solved with a little creativity, however, by giving every menu item a focusable element. For example:

然而,通过为每个菜单项提供可聚焦元素,可以通过一点创造力来解决可访问性问题。例如:

<MenuItem Focusable="False">
    <MenuItem.Header>
        <StackPanel Orientation="Horizontal" Focusable="True" FocusVisualStyle="{x:Null}">
            <TextBlock Text="Do something!" />
        </StackPanel>
    </MenuItem.Header>
</MenuItem>

The FocusVisualStyle="{x:Null}" code is required to hide the dotted focus that would otherwise appear (and looks out of place in a menu).

需要使用FocusVisualStyle =“{x:Null}”代码来隐藏否则会出现的虚线焦点(并且在菜单中看起来不合适)。

#3


0  

Maybe this helps:

也许这有助于:

    <MenuItem StaysOpenOnClick="True">
        <MenuItem.Header>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Content="Some value:" Margin="0,3,6,0" Padding="0" />
                <TextBox Margin="0,0,0,6" Grid.Column="1" />
            </Grid>
        </MenuItem.Header>
    </MenuItem>

#1


6  

As I wrote I am not sure how you use your menu control. But maybe this code snipped can help you or give you a hint:

正如我写的,我不知道你如何使用菜单控件。但也许这段代码可以帮助你或给你一个提示:

<TextBox Margin="0,0,0,6" Grid.Column="1" PreviewLostKeyboardFocus="OnPreviewLostKeyboardFocus"/>

and the according method:

和相应的方法:

private void OnPreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  {
     if (MainMenu.IsKeyboardFocusWithin)
     {
        e.Handled = true;
     }
  }

#2


1  

I know this answer is probably way too late to help you, but maybe it'll help anyone searching for fixes to this issue.

我知道这个答案可能为时已晚,无法帮助您,但也许它可以帮助任何人找到解决此问题的方法。

Setting Focusable=false on the MenuItems also works. It still allows them to be clicked and allows their contained controls to have focus. However, it disables the ability to navigate the menu using just the keyboard, introducing an accessibility issue.

在MenuItems上设置Focusable = false也可以。它仍然允许单击它们并允许其包含的控件具有焦点。但是,它禁用了仅使用键盘导航菜单的功能,从而引入了辅助功能问题。

The accessibility issue can be solved with a little creativity, however, by giving every menu item a focusable element. For example:

然而,通过为每个菜单项提供可聚焦元素,可以通过一点创造力来解决可访问性问题。例如:

<MenuItem Focusable="False">
    <MenuItem.Header>
        <StackPanel Orientation="Horizontal" Focusable="True" FocusVisualStyle="{x:Null}">
            <TextBlock Text="Do something!" />
        </StackPanel>
    </MenuItem.Header>
</MenuItem>

The FocusVisualStyle="{x:Null}" code is required to hide the dotted focus that would otherwise appear (and looks out of place in a menu).

需要使用FocusVisualStyle =“{x:Null}”代码来隐藏否则会出现的虚线焦点(并且在菜单中看起来不合适)。

#3


0  

Maybe this helps:

也许这有助于:

    <MenuItem StaysOpenOnClick="True">
        <MenuItem.Header>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Content="Some value:" Margin="0,3,6,0" Padding="0" />
                <TextBox Margin="0,0,0,6" Grid.Column="1" />
            </Grid>
        </MenuItem.Header>
    </MenuItem>