14

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?

2
  • 2
    use Activated event of your window Commented Jun 18, 2011 at 11:30
  • just to be picky: this is not a user interface guideline but from user experience users do not like messing with what they have set. Ie. if I set focus to a textbox then alt-tab out to copy some value if you hide and show the form I expect to be still in that textbox. But of course your question can be inspired by a user requirement. in that case just forget everything :) Commented Jun 18, 2011 at 11:48

2 Answers 2

33

Just call Element.Focus() on the element you want to focus.

If you mean bring focus to the window try calling window.Activate()

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.