简体   繁体   English

WPF窗口设置焦点

[英]WPF Window set Focus

I have a WPF Window which I only create one time and then Show() and Hide() several times. 我有一个WPF窗口,我只创建一次,然后多次Show()和Hide()。 Now I am searching a way to set the focus on a element on each Show(). 现在我正在寻找一种方法来将焦点设置在每个Show()上的元素上。 Where and how can I do this? 我在哪里以及如何做到这一点?

Just call Element.Focus() on the element you want to focus. 只需在要聚焦的元素上调用Element.Focus()即可。

If you mean bring focus to the window try calling window.Activate() 如果你的意思是把焦点带到窗口尝试调用window.Activate()

In WPF there are two main concepts that pertain to focus: keyboard focus and logical focus. 在WPF中,有两个与焦点相关的主要概念:键盘焦点和逻辑焦点。 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. 您可以通过包含Window.Show/Hide和element.Focus功能来编写自己的ShowHide方法,以便在同一方法中处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM