Set focus on textbox in WPF

后端 未结 9 816
-上瘾入骨i
-上瘾入骨i 2020-11-28 08:03

How to set the focus on an TextBox element in WPF

I have this code:

txtCompanyID.Focusable = true;
txtCompanyID.Focus();
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 08:13

    None of this worked for me as I was using a grid rather than a StackPanel.

    I finally found this example: http://spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/

    and modified it to this:

    In the 'Resources' section:

        
    

    In my grid definition:

    
    

提交回复
热议问题