C# – Set focus on textbox in WPF

cfocusnettextboxwpf

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?

Best Answer

In XAML:

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