C# WPF 保持键盘焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/512902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:18:52  来源:igfitidea点击:

WPF Maintain Keyboard Focus

c#wpfkeyboardfocus

提问by Joseph Sturtevant

I'm creating a UserControlconsisting of a TextBoxand a ListView. I want keyboard focus to remain with the TextBoxas long as the control has keyboard focus (selection changes in the ListViewshouldn't remove keyboard focus from the TextBox).

我正在创建一个UserControl由 aTextBox和 a组成的ListViewTextBox只要控件具有键盘焦点,我就希望键盘焦点保持在中( 中的选择更改ListView不应从 中删除键盘焦点TextBox)。

I've tried catching GotKeyboardFocusin the ListViewand passing keyboard focus back to the TextBoxusing Keyboard.Focus(),but this seems to cancel any selection operation in the ListView. The below code shows the problem. Does anyone know how to achieve this functionality?

我试着追赶GotKeyboardFocusListView并通过键盘焦点回TextBoxKeyboard.Focus(),但这似乎要取消任何选择操作ListView。下面的代码显示了问题。有谁知道如何实现这个功能?

<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TextBox x:Name="TextBox1" />
        <ListView x:Name="ListBox1" Keyboard.GotKeyboardFocus="ListBox1_GotKeyboardFocus">
            <ListViewItem Content="Able" />
            <ListViewItem Content="Baker" />
            <ListViewItem Content="Charlie" />
        </ListView>
    </StackPanel>
</Window>


using System.Windows;
using System.Windows.Input;

namespace WpfApplication5
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void ListBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Keyboard.Focus(TextBox1);
        }
    }
}

采纳答案by Bubblewrap

It looks like it's possible to change focus in the MouseUp event. I think if you do it too early, like in the GotKeyboardFocus event, you'll steal focus before the listview can handle the event and select the chosen item.

看起来可以在 MouseUp 事件中更改焦点。我认为如果你做得太早,比如在 GotKeyboardFocus 事件中,你会在列表视图处理事件并选择所选项目之前窃取焦点。

<StackPanel>
    <TextBox x:Name="TextBox1" />
    <ListView x:Name="ListBox1" MouseUp="ListBox1_MouseUp">
        <ListViewItem Content="Able" />
        <ListViewItem Content="Baker" />
        <ListViewItem Content="Charlie" />
    </ListView>
</StackPanel>

private void ListBox1_MouseUp(object sender, MouseButtonEventArgs e)
{
    TextBox1.Focus();
}

回答by Szymon Rozga

This is a hack, but what if instead of listening to the GotKeyboardFocus event, you listen to the SelectionChanged event on the ListBox?

这是一个 hack,但是如果不是监听 GotKeyboardFocus 事件,而是监听 ListBox 上的 SelectionChanged 事件呢?

回答by Adrian

Instead, have you considered just capturing keystrokes and putting those keystrokes into your TextBox?

相反,您是否考虑过只捕获击键并将这些击键放入您的文本框?

<Window PreviewKeyDown="Window_PreviewKeyDown" >
    <Grid>
        <TextBox x:Name="TextBox1" />
        <ListBox />
    </Grid>
</Window>

Then in your window's code-behind:

然后在您窗口的代码隐藏中:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
   TextBox1.Text += e.Key.ToString();
}

You'll have to do extra work for anything like special characters (backspace, etc), and obviously a Key handler for your "Enter" or "Post" operation, but it gives you the ability to just free-form type while the Window has focus and to properly handle the keystrokes as necessary.

您必须为特殊字符(退格符等)等任何东西做额外的工作,显然是您的“输入”或“发布”操作的键处理程序,但它使您能够在窗口中自由输入具有焦点并在必要时正确处理击键。

回答by Bubblewrap

Put Focusable=false on your ListView.

将 Focusable=false 放在您的 ListView 上。

回答by IceX

If you are calling your WPF window from a WinForm you must use this:

如果您从 WinForm 调用 WPF 窗口,则必须使用以下命令:

System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfWindow);
wpfWindow.show();

from the MSDN documentation.

来自MSDN 文档

Thats how I solved my keyboard problem.

这就是我解决键盘问题的方法。

IceX

爱思克斯

回答by mike a

Ok, this was driving me crazy. Even though set focus to UserControlevery time lost focus, still couldn't get my command hot keys to work. All I had to do was to set the property Focusableto true, and voilà, it's working!

好吧,这让我发疯了。即使将焦点设置为UserControl每次失去焦点时,仍然无法使我的命令热键起作用。我所要做的就是将属性设置Focusabletrue,瞧,它起作用了!