如何在WPF中移动焦点? [英] How to move focus in WPF?
本文介绍了如何在WPF中移动焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道是否有改变从当前控件的焦点,并将其移动到TabIndex分配控件的WPF中的其他控件。
示例
I有一个TabIndex 1到5的控件,有没有办法让焦点从1到5?
< TextBox TabIndex = 1Focusable =trueLostFocus =test_LostFocus/>
< TextBox TabIndex =2Focusable =true/>
...
< TextBox TabIndex =5Focusable =truename =LastControl/>
。
private void test_LostFocus(object sender,RoutedEventArgs e)
{
LastControl.Focus();
$ b我尝试了 Keyboard.Focus()
和 FocusManager.SetFocusedElement()
但没有运气。
有什么想法吗?
$只需处理文本框的 KeyDown
事件并在那里设置焦点。既然你使用 Tab
,让控件知道你将通过设置 e.Handled = true
来处理它,这将停止使用下一个 TabIndex
跳转到控件的默认选项卡
。 b
private void tb_KeyDown(object sender,KeyEventArgs e)
{
if(e.Key == Key.Tab)
{
e.Handled = true;
LastControl.Focus();
}
}
I wonder if there is anyway to change focus from the current control and move it to other control in WPF on TabIndex assigned controls.
Example
I have controls with TabIndex 1 to 5, is there a way to jumb the focus from 1 to 5 ?
<TextBox TabIndex="1" Focusable = "true" LostFocus="test_LostFocus"/>
<TextBox TabIndex="2" Focusable = "true"/>
...
<TextBox TabIndex="5" Focusable = "true" name="LastControl"/>
.
private void test_LostFocus(object sender, RoutedEventArgs e)
{
LastControl.Focus();
}
I tried Keyboard.Focus()
and FocusManager.SetFocusedElement()
but no luck.
Any idea?
解决方案 Simply handle the KeyDown
event for the textboxes and set focus there. Since you're using Tab
, let the control know you'll handle it by setting e.Handled = true
, which will stop the default tab
action of jumping to the control with the next TabIndex
.
private void tb_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
e.Handled = true;
LastControl.Focus();
}
}
这篇关于如何在WPF中移动焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文