Set focus on textbox in WPF

priyanka.sarkar picture priyanka.sarkar · Aug 28, 2009 · Viewed 148.3k times · Source

How to set the focus on an TextBox element in WPF

I have this code:

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

...but it is not working.

Any idea?

Answer

usefulBee picture usefulBee · Feb 19, 2014

In XAML:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=Box}">
   <TextBox Name="Box" />
</StackPanel>