简体   繁体   English

AutoCompleteBox在CTRL + N键事件上选择但未聚焦

[英]AutoCompleteBox Selected on CTRL + N key event but not focused

I want to focus AutoCompleteBox when on UserControl CTRL + N key pressed. 我想在按UserControl CTRL + N键时将AutoCompleteBox聚焦。 I wrote Keyboard.Focus(CustomerSearch); 我写了Keyboard.Focus(CustomerSearch); . The problem is on pressing CTRL + N key, the AutoCompleteBox not focused but selected with dotted line as can seen in below image, 问题在于按下CTRL + N键时,AutoCompleteBox没有聚焦,而是用虚线选中,如下图所示,
在此处输入图片说明

 <controls:AutoCompleteBox Name="CustomerSearch" IsTextCompletionEnabled="True" SelectedItem="{Binding Name, Mode=TwoWay}" Grid.Column="1" PreviewKeyDown="CustomerSearch_PreviewKeyDown" >
                    <controls:AutoCompleteBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Code}"/>
                                <TextBlock Text="{Binding Name}"/>
                                <TextBlock Text="{Binding Address}"/>
                                <TextBlock Text="{Binding Contact}"/>
                            </StackPanel>
                        </DataTemplate>
                    </controls:AutoCompleteBox.ItemTemplate>
                </controls:AutoCompleteBox>

The Ctrol + N event: Ctrol + N事件:

private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                Keyboard.Focus(CustomerSearch);

            }
        }

You should focus the TextBox in the AutoCompleteBox : 您应该将TextBox放在AutoCompleteBox

private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        TextBox textBox = CustomerSearch.Template.FindName("Text", CustomerSearch) as TextBox;
        if (textBox != null)
            Keyboard.Focus(textBox);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM