WPF and initial focus

后端 未结 12 2190
鱼传尺愫
鱼传尺愫 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条回答
  •  鱼传尺愫
    2020-11-29 16:34

    Above solution was not working as expected for me, I've changed slightly the behavior proposed by Mizipzor as following:

    From this part

    if ((bool)args.NewValue)
            {
                control.Loaded += (sender, e) =>
                       control.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
    

    To this

    if ((bool)args.NewValue)
            {
                control.Loaded += (sender, e) => control.Focus();
            }
    

    ANd I'm not attaching this behavior to Window or UserControl, but to control I want to focus initially, e.g.:

    
    

    Oh, sorry for different naming I'm using InitialFocus name for the attached property.

    And this is working for me, maybe it could help someone else.

提交回复
热议问题