通过绑定设置文本后,如何将焦点设置到文本框?
我正在尝试为 WPF 应用程序中的所有文本框实现 SelectAll(文本)功能。我找到了如何做到这一点 这里。但是,我的表单上的第一个文本框不会自动聚焦。我尝试通过简单地使用 firstTextBox.Focus
在 Window_Loaded 事件处理程序中解决这个问题。这可行,但此 TextBox 的 Text 属性是通过绑定设置的,并且这似乎发生在 Window_Loaded 事件之后。因此,我最终得到了第一个文本框,该文本框最初是聚焦的,但没有选择其文本。看来我需要连接到另一个事件。哪一个?
I am trying to implement SelectAll (text) functionality for all TextBoxes in my WPF application. I found how to do that here. However, the first TextBox on my form is not automatically focused. I try to remedy that in the Window_Loaded eventhandler by simply using firstTextBox.Focus
. That works, but the Text property of this TextBox is set through binding and it seems that this happens after the Window_Loaded event. So, I end up with a first textbox that is initially focused, but has not its Text selected. It seems I need to hook up to a different Event. Which one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不尝试 DataContextChangedEvent。在 WPF 中,获得焦点始终是一件痛苦的事情...我们必须为其传递代码隐藏文件...
Why don't you try DataContextChangedEvent. Bringing focus is always a pain in WPF... we have to relay on code behind file for it...
尝试在窗口的 XAML 中使用 FocusManager:
Try using FocusManager in the XAML of your window: