无法设置元素的焦点
我有一个 Expander,其内容由 StackPanel
组成,其中包含多个元素,其中一个元素是 TextBox
。
我想,当 Expander
扩展 TextBox
时应该获得键盘焦点,我该怎么做?
我尝试过:
Private Sub xp_Expanded(sender As Object, e As RoutedEventArgs) _
Handles xpUnits.Expanded
stackPanel.Focus()
Keyboard.Focus(textBox)
textBox.Focus()
End Sub
我什至尝试将 FocusManager.IsFocusable
和 FocusManager.FocusedElement
设置为 TextBox
,然后调用 stackPanel.Focus()
,但它没有完成这项工作。
I have an Expander that its content consists of a StackPanel
that contains several elements one of whom is a TextBox
.
I want, that when the Expander
expands that TextBox
should gain keyboard focus, how do I do this?
I tried:
Private Sub xp_Expanded(sender As Object, e As RoutedEventArgs) _
Handles xpUnits.Expanded
stackPanel.Focus()
Keyboard.Focus(textBox)
textBox.Focus()
End Sub
I even tried to set FocusManager.IsFocusable
and FocusManager.FocusedElement
to the TextBox
, then call stackPanel.Focus()
, but it didn't do the job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您尝试设置焦点时,您的
TextBox
可能还不可见。您应该将IsVisibleChanged
的事件处理程序添加到TextBox
并在那里设置焦点。在 xp_Expanded 内,您只需设置一个布尔标志,以便下次调用 IsVisibleChanged 事件处理程序时应将焦点集中在TextBox
上。Probably your
TextBox
is not yet visible when you try to set the focus. You should add an event handler forIsVisibleChanged
to yourTextBox
and set the focus there. Insidexp_Expanded
you just should set a boolean flag that theTextBox
should be focused the next time theIsVisibleChanged
event handler is called.这个答案解决了我的问题轻松发出:
This answer solved my issue easily: