Search code examples
c#wpfc#-4.0user-controlswpf-controls

How to focus control in the tabItem in WPF


I have tabControl in the form. In one of the tabItems I have textbox(myTextBox). Let's call it tabItem1. When I write something into the this text box placed in the tabItem1 I want to focus textbox(searchTextBox) in the tabItem2. I placed this code in the KeyDown of the

        tabItem2.Focus();
        searchTextBox.Text = searchTextBoxTeropatik.Text;

        searchTextBox.Focus();

I wrote this small function for this purpose. But there is a big problem.

  1. I press the Key

  2. tabItem2 gets the focus.

But searchTextBox does not get the focus.(My problem)


Solution

  • Call UpdateLayout() after focusing the second TabItem so the system gets the time to redraw the tab.

      tabItem2.Focus();
      UpdateLayout();
      searchTextBox.Focus();