Can't set focus to a child of UserControl

后端 未结 18 613
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题