2

How can I mark textBox as a default focus ?

When the windows is start , when the user press something it will be write on this textbox

Thanks

  • 2
    Possible duplicate of [Set the focus on a textbox in xaml wpf](https://stackoverflow.com/questions/2872238/set-the-focus-on-a-textbox-in-xaml-wpf) – Dag Baardsen Oct 22 '17 at 20:11

1 Answers1

3

Bind the FocusManager.FocusedElement attached property of the root panel to the TextBox:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=tb}">
    <TextBox x:Name="tb" Background="Beige" Text="..."/>
</StackPanel>

This will will work if you have a single focus scope, or if the focus scope to which the TextBox belongs currently has keyboard focus.

You could also use the Keyboard.Focus method to set the keyboard focus:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += (s, e) => Keyboard.Focus(tb);
}
}

There can be only one element on the whole desktop that has keyboard focus. Please refer to MSDN for more information.

Focus Overview: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/focus-overview

mm8
  • 163,881
  • 10
  • 57
  • 88