如何在winApp中将焦点放在组合框的下拉列表中 [英] How to set focus on dropdownlist of a combobox in winApp

查看:93
本文介绍了如何在winApp中将焦点放在组合框的下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我陷入了一件小事。我正在winApp中工作。我想把重点放在下拉列表中(dropdownstyle属性从文本框中按下输入的同时,只需从文本框中移动时,我的焦点应该放在组合框上。



所以请一些人帮助我。



谢谢,

Hi there,

I am stuck in a small thing.I am doing my work in winApp.I want to set the focus on dropdownlist(dropdownstyle Property of a combobox)while pressing enter from a textbox.so simply while I moved from a textbox my focus should go to the combobox.

so please some body help me out.

Thanks,

推荐答案

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        this.comboBox1.Focus();
}



会让你进入组合框。我建议comboBox的TabIndex属性也设置为文本框的TabIndex加1


will get you to the combobox. I would advise that the TabIndex property of the comboBox is also set to the the TabIndex of the text box plus 1


您可以尝试更通用的方法,它处理所有TextBox上的这种特定行为。

只需将此代码放入App.xaml.cs代码后面:

You can try more generic approach, which handles this specific behaviour on all TextBoxes.
Simply put this code into App.xaml.cs code behind:
/// <summary>
/// Handles the Startup event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
private void Application_Startup(object sender, StartupEventArgs e)
{
    // Attach to global PreviewKeyDown Event
    EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewKeyDownEvent, new RoutedEventHandler(TextBox_PreviewKeyDown));
}

/// <summary>
/// Handles the PreviewKeyDown event of the TextBox control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private static void TextBox_PreviewKeyDown(object sender, RoutedEventArgs e)
{
    if ((e as KeyEventArgs).Key == Key.Enter) // Enter pressed
    {
        if ((e.Source as TextBox).AcceptsReturn) // Ignore when TextBox accepts enter key
            return;

        // Get currently focused element
        var focusedElem = Keyboard.FocusedElement as UIElement;

        if (focusedElem != null)
        {
            // Focus next element
            focusedElem.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

            // Check if next element is ComboBox
            var comboBox = Keyboard.FocusedElement as ComboBox;

            // Open DropDown
            if (comboBox != null)
                comboBox.IsDropDownOpen = true;
        }

        e.Handled = true;
    }
}



此外,我的示例包含自动打开组合框下拉列表的代码。


Also my example contains code for auto opening combo box drop down.


这篇关于如何在winApp中将焦点放在组合框的下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆