猿问

在 UserControl 中按下的 CTRL+ N 键聚焦到 AutoCompleteBox

我想在AutocompleteBox按下 CTRL+N 键时聚焦。我尝试了一些代码,但对我不起作用。在UserControl控制中,我使用PreviewKeyDown了如下事件,


注意:只有在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"

                 >


上面的代码对我不起作用。请帮忙


动漫人物
浏览 174回答 1
1回答

HUH函数

您可以尝试使用DispatcherPriority低于以下值的调度程序Normal:private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e){&nbsp; &nbsp; if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Keyboard.Focus(SearchTextBox);&nbsp; &nbsp; &nbsp; &nbsp; SearchTextBox.Dispatcher.BeginInvoke(new Action(() =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SearchTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));&nbsp; &nbsp; &nbsp; &nbsp; }), System.Windows.Threading.DispatcherPriority.Background);&nbsp; &nbsp; &nbsp; &nbsp; e.Handled = true;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答