收缩儿童在收缩时会获得键盘焦点吗? [英] Expander children get keyboard focus when contracted?
问题描述
为什么添加到Expander控件的孩子在看不见时(未扩展Expander时)仍能获得键盘焦点?有什么办法可以关闭该行为?
Why do children added to an Expander control still get keyboard focus when they're not visible (when the Expander is NOT expanded)?? Is there some way to turn off that behavior?
推荐答案
我复制了一个基于您上面提到的.实际上,当您使用鼠标单击Expander使其折叠时,Expander模板中的 ToggleButton会获得键盘焦点.如果要将焦点移到其他UI元素,可以调用Keyboard.Focus方法.
I reproduced a small example based on what you mentioned above. Actually, when you use mouse click the Expander to collapse it, then the ToggleButton inside in template of Expander get keyboard focus. If you want to move focus to other UI elements, you can invoke Keyboard.Focus method.
XAML代码:
< > 跨度>
< 页眉
="expander"
Name="expander"
="onCollapsed"
Collapsed="OnCollapsed"
=" True">
IsExpanded="True" >
< >
< 文本 =" textBox1" />
< 文本 =" textBox2"
<TextBox Text="Text2" Name="textBox2" />
< 文本 =" textBox3"
<TextBox Text="Text3" Name="textBox3" />
</ >
</ >
< 文本 =" textBox4" />
</ StackPanel
在后面的代码中:
public 部分 Window1 :
public partial class Window1 : Window
{
公共 Window1()
public Window1()
{
InitializeComponent();
此.已加载+ =
this.Loaded += delegate
{
textBox2.Focus();
};
}
私有 对象发件人,
private void OnCollapsed(object sender, RoutedEventArgs e)
{
var 元素=
var element = Keyboard.FocusedElement;
如果(元素!=
if (element != null)
{
///现在是Expander中的ToggleButton获取键盘焦点
//now is the ToggleButton inside the Expander get keyboard focus
MessageBox .Show(element.GetType().ToString());
MessageBox.Show(element.GetType().ToString());
}
//移动焦点
//move focus
键盘 .Focus(textBox4);
Keyboard.Focus(textBox4);
}
}
有关更多信息,请提供一个小例子更具体地说明您的情况?
谢谢.
这篇关于收缩儿童在收缩时会获得键盘焦点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!