Set focus to text box in WPF

How to set focus on a TextBox in WPF

I have this code:

 txtCompanyID.Focusable = true; txtCompanyID.Focus(); 

... but it does not work.

Any idea?

+95
c # wpf focus textbox
Aug 28 '09 at 6:44
source share
9 answers

In XAML:

 <StackPanel FocusManager.FocusedElement="{Binding ElementName=Box}"> <TextBox Name="Box" /> </StackPanel> 
+128
Feb 19 '14 at 20:03
source share

try FocusManager.SetFocusedElement

 FocusManager.SetFocusedElement(parentElement, txtCompanyID) 
+49
Aug 28 '09 at 6:50
source share

No one has yet explained why the code in the question is not working. I assume that the code was placed in a window constructor. But at this time it is too early to establish focus. This must be done as soon as the Window is ready for interaction. The best place for the code is the Loaded event:

 public KonsoleWindow() { public TestWindow() { InitializeComponent(); Loaded += TestWindow_Loaded; } private void TestWindow_Loaded(object sender, RoutedEventArgs e) { txtCompanyID.Focus(); } } 
+45
Jul 04 '16 at 9:17
source share
 txtCompanyID.Focusable = true; Keyboard.Focus(txtCompanyID); 

MSDN:

There can be only one item on the entire desktop with a keyboard. In WPF, an element with a keyboard focus will have IsKeyboardFocused set to true.

You can break through the settings line and check the value of the IsKeyboardFocused property. Also check if you really reached this line, or maybe you set another element to get focus after that.

+22
Aug 28 '09 at 6:53
source share

Try the following: MyTextBox.Focus ( );

+16
Mar 07 '14 at 14:40
source share

None of this worked for me since I used a grid, not a StackPanel.

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

and changed it to this:

In the Resources section:

  <Style x:Key="FocusTextBox" TargetType="Grid"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=textBoxName, Path=IsVisible}" Value="True"> <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=textBoxName}"/> </DataTrigger> </Style.Triggers> </Style> 

In my grid definition:

 <Grid Style="{StaticResource FocusTextBox}" /> 
+10
Jun 01 '15 at 10:33
source share

I was a bit late for the game. I tried the solutions presented here, but they did not work. If anyone else has trouble adjusting the focus, you can try this.

 <TextBox behave:UserControlBehavior.FocusFirst='True' Text="Hello World!" /> 
0
Dec 13 '18 at 20:15
source share

Another possible solution is to use FocusBehavior provided by the free DevExpress MVVM Framework :

 <TextBox Text="This control is focused on startup"> <dxmvvm:Interaction.Behaviors> <dxmvvm:FocusBehavior/> </dxmvvm:Interaction.Behaviors> </TextBox> 

It allows you to focus the control when it loads, when a specific event occurs or when a property changes.

0
Apr 04 '19 at 10:18
source share

If you did not find a solution to the other answers, then how I solved the problem.

 Application.Current.Dispatcher.BeginInvoke(new Action(() => { TEXTBOX_OBJECT.Focus(); }), DispatcherPriority.Render); 

From what I understand, other solutions may not work, because the Focus() call is called before the application displays the other components.

0
May 24 '19 at 9:23 AM-
source share



All Articles