Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 293
Tabriz Atayi Avatar asked Nov 25 '11 08:11

Tabriz Atayi


People also ask

How do you focus a control in WPF?

An element can be turned into a focus scope in Extensible Application Markup Language (XAML) by setting the FocusManager attached property IsFocusScope to true . In code, an element can be turned into a focus scope by calling SetIsFocusScope.

What is Tab Control WPF?

The WPF TabControl allows you to split your interface up into different areas, each accessible by clicking on the tab header, usually positioned at the top of the control. Tab controls are commonly used in Windows applications and even within Windows' own interfaces, like the properties dialog for files/folders etc.


1 Answers

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

  tabItem2.Focus();
  UpdateLayout();
  searchTextBox.Focus();
like image 160
SvenG Avatar answered Sep 19 '22 03:09

SvenG