WPF 窗口设置焦点
我有一个 WPF 窗口,我只创建一次,然后 Show() 和 Hide() 多次。现在我正在寻找一种方法来将焦点设置在每个 Show() 上的元素上。我可以在哪里以及如何执行此操作?
I have a WPF Window which I only create one time and then Show() and Hide() several times. Now I am searching a way to set the focus on a element on each Show(). Where and how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需对要聚焦的元素调用 Element.Focus() 即可。
如果您的意思是将焦点移至窗口,请尝试调用 window.Activate()
Just call Element.Focus() on the element you want to focus.
If you mean bring focus to the window try calling window.Activate()
在 WPF 中,有两个与焦点相关的主要概念:键盘焦点和逻辑焦点。键盘焦点是指接收键盘输入的元素,逻辑焦点是指焦点范围内具有焦点的元素。 此概述中详细讨论了这些概念。
您可以通过包含 Window.Show/Hide 和 element.Focus 功能来编写自己的 ShowHide 方法,以便在同一方法中处理这两个功能。
In WPF there are two main concepts that pertain to focus: keyboard focus and logical focus. Keyboard focus refers to the element that receives keyboard input and logical focus refers to the element in a focus scope that has focus. These concepts are discussed in detail in this overview.
You can probably write you own ShowHide method by including both Window.Show/Hide and element.Focus functionalities in order to handle both in the same method.