WPF and initial focus

后端 未结 12 2175
鱼传尺愫
鱼传尺愫 2020-11-29 15:59

It seems that when a WPF application starts, nothing has focus.

This is really weird. Every other framework I\'ve used does just what you\'d expect: puts initial foc

12条回答
  •  萌比男神i
    2020-11-29 16:25

    I had the bright idea to dig through Reflector to see where the Focusable property is used, and found my way to this solution. I just need to add the following code to my Window's constructor:

    Loaded += (sender, e) =>
        MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
    

    This will automatically select the first control in the tab order, so it's a general solution that should be able to be dropped into any window and Just Work.

提交回复
热议问题