无法设置元素的焦点

发布于 2024-09-24 05:46:23 字数 558 浏览 7 评论 0原文

我有一个 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.IsFocusableFocusManager.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-10-01 05:46:23

当您尝试设置焦点时,您的 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 for IsVisibleChanged to your TextBox and set the focus there. Inside xp_Expanded you just should set a boolean flag that the TextBox should be focused the next time the IsVisibleChanged event handler is called.

成熟的代价 2024-10-01 05:46:23

这个答案解决了我的问题轻松发出:

<TextBox Text="{Binding Title}"
  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>

This answer solved my issue easily:

<TextBox Text="{Binding Title}"
  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文