如何在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.