我想在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" >上面的代码对我不起作用。请帮忙
1 回答
HUH函数
TA贡献1836条经验 获得超4个赞
您可以尝试使用DispatcherPriority低于以下值的调度程序Normal:
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;
}
}
- 1 回答
- 0 关注
- 173 浏览
添加回答
举报
0/150
提交
取消