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?

CharithJ
  • 46,289
  • 20
  • 116
  • 131
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 2
    use Activated event of your window – Navid Rahmani Jun 18 '11 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 :) – Marino Šimić Jun 18 '11 at 11:48

2 Answers2

33

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

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

Cilvic
  • 3,417
  • 2
  • 33
  • 57
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.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131