Can't set focus to a child of UserControl

后端 未结 18 574
耶瑟儿~
耶瑟儿~ 2020-12-04 21:55

I have a UserControl which contains a TextBox. When my main window loads I want to set the focus to this textbox so I added Focusable=\"True

相关标签:
18条回答
  • 2020-12-04 22:31

    “When setting initial focus at application startup, the element to receive focus must be connected to a PresentationSource and the element must have Focusable and IsVisible set to true. The recommended place to set initial focus is in the Loaded event handler" (MSDN)

    Simply add a "Loaded" event handler in the constructor of your Window (or Control), and in that event handler call the Focus() method on the target control.

        public MyWindow() {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MyWindow_Loaded);
        }
    
        void MyWindow_Loaded(object sender, RoutedEventArgs e) {
            textBox.Focus();
        }
    
    0 讨论(0)
  • 2020-12-04 22:31

    I have user control - stack panel with two text boxes.The text boxes were added in contructor, not in the xaml. When i try to focus first text box, nothing happend. The siggestion with Loaded event fix my problem. Just called control.Focus() in Loaded event and everthing.

    0 讨论(0)
  • 2020-12-04 22:33

    I had same problem with setting keyboard focus to canvas in WPF user control. My solution

    1. In XAML set element to Focusable="True"
    2. In element_mousemove event create simple check:

      if(!element.IsKeyBoardFocused)
          element.Focus();
      

    In my case it works fine.

    0 讨论(0)
  • 2020-12-04 22:35

    WPF supports two different flavors of focus:

    1. Keyboard focus
    2. Logical focus

    The FocusedElement property gets or sets logical focus within a focus scope. I suspect your TextBox does have logical focus, but its containing focus scope is not the active focus scope. Ergo, it does not have keyboard focus.

    So the question is, do you have multiple focus scopes in your visual tree?

    0 讨论(0)
  • 2020-12-04 22:35

    I found a good series of blog posts on WPF focus.

    • Part 1: It’s Basically Focus
    • Part 2: Changing WPF focus in code
    • Part 3: Shifting focus to the first available element in WPF

    They are all good to read, but the 3rd part specifically deals with setting focus to a UI element in a UserControl.

    0 讨论(0)
  • 2020-12-04 22:36

    since i tried a fuzquat's solution and found it the most generic one, i thought i'd share a different version, since some complained about it looking messy. so here it is:

                    casted.Dispatcher.BeginInvoke(new Action<UIElement>(x =>
                    {
                        x.Focus();
                    }), DispatcherPriority.ApplicationIdle, casted);
    

    no Thread.Sleep, no ThreadPool. Clean enough i hope. Could really use some rep so i can comment on other peoples solutions.

    UPDATE:

    Since people seem to like pretty code:

    public static class WpfExtensions
    {
        public static void BeginInvoke<T>(this T element, Action<T> action, DispatcherPriority priority = DispatcherPriority.ApplicationIdle) where T : UIElement
        {
            element.Dispatcher.BeginInvoke(priority, action);
        }
    }
    

    now you can call it like this:

    child.BeginInvoke(d => d.Focus());
    
    0 讨论(0)
提交回复
热议问题