Set focus on textbox in WPF

后端 未结 9 819
-上瘾入骨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:17

    In Code behind you can achieve it only by doing this.

     private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                txtIndex.Focusable = true;
                txtIndex.Focus();
            }
    

    Note: It wont work before window is loaded

提交回复
热议问题