Can't set focus to a child of UserControl

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

    1. Set your user control to Focusable="True" (XAML)
    2. Handle the GotFocus event on your control and call yourTextBox.Focus()
    3. Handle the Loaded event on your window and call yourControl.Focus()

    I have a sample app running with this solution as I type. If this does not work for you, there must be something specific to your app or environment that causes the problem. In your original question, I think the binding is causing the problem. I hope this helps.

提交回复
热议问题