C# 在CTRL+;在用户控件中按N键

C# 在CTRL+;在用户控件中按N键,c#,wpf,C#,Wpf,按下CTRL+N键时,我想将注意力集中到自动完成框。我尝试了一些代码,但不适合我。在UserControlcontrol中,我使用了PreviewKeyDown事件,如下所示 private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control)

按下CTRL+N键时,我想将注意力集中到
自动完成框
。我尝试了一些代码,但不适合我。在
UserControl
control中,我使用了
PreviewKeyDown
事件,如下所示

private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
            {
if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
MessageBox.Show("sfsd");
                Keyboard.Focus(SearchTextBox);
                SearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                e.Handled = true;
            }
注意:我只在写
MessageBox.Show(“some”)时才关注下面的代码键事件之前,如下所示

private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
            {
if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
MessageBox.Show("sfsd");
                Keyboard.Focus(SearchTextBox);
                SearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                e.Handled = true;
            }
用户控制:

 <UserControl x:Class="Inventory_Control.UserControls.SaleTab"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Inventory_Control.UserControls"
                 xmlns:staticData="clr-namespace:Inventory_Control.UserControls"
                 mc:Ignorable="d" 
                 xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
                 d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded_1" PreviewKeyDown="UserControl_PreviewKeyDown"
                 >

上述代码对我不起作用。请帮助

您可以尝试使用优先级低于
正常
的调度程序:

private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        Keyboard.Focus(SearchTextBox);
        SearchTextBox.Dispatcher.BeginInvoke(new Action(() =>
        {
            SearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }), System.Windows.Threading.DispatcherPriority.Background);
        e.Handled = true;
    }
}

当你们调试它的时候,你们的代码是否会全部通过?是的,它会通过我从另一个问题得到答案
private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        Keyboard.Focus(SearchTextBox);
        SearchTextBox.Dispatcher.BeginInvoke(new Action(() =>
        {
            SearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }), System.Windows.Threading.DispatcherPriority.Background);
        e.Handled = true;
    }
}