代码之家  ›  专栏  ›  技术社区  ›  Shimmy Weitzhandler 500 - Internal Server Error

无法设置元素的焦点

  •  1
  • Shimmy Weitzhandler 500 - Internal Server Error  · 技术社区  · 14 年前

    我有一个扩展器,它的内容包括 StackPanel 包含几个元素,其中一个元素是 TextBox .

    Expander 扩大了范围 文本框 应该获得键盘焦点,我该怎么做?

    我试过:

    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 stackPanel.Focus() ,但没有起作用。

    2 回复  |  直到 14 年前
        1
  •  4
  •   user128300 user128300    14 年前

    TextBox 在您尝试设置焦点时还不可见。您应该为添加一个事件处理程序 IsVisibleChanged 给你的 把焦点放在那里。内部 xp_Expanded 您只需设置一个布尔标志 应该在下一次 是否可见更改 调用事件处理程序。

        2
  •  0
  •   Community CDub    7 年前

    This answer

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