How can I set the initially focused element without activating the window (WPF)?
My program creates a window with ShowActivated=false
, so that it doesn't steal keyboard focus when it's opened (the window is created in response to events coming over the netwo开发者_StackOverflow社区rk). I want to be able to specify that a particular TextBox field receives focus when the user switches to the window. However, the seemingly obvious methods (using FocusManager.FocusedElement
in the window's markup, or calling this.textbox.Focus()
when the window is shown) have the side effect of activating the window.
I ended up adding an event handler for the window's Activated
event, which (a) makes the desired Focus()
call, and (b) removes itself from the Activated
event, so that it only happens once. This seems crufty. Am I missing something?
精彩评论