I am using the Text box in the user control which will be displayed as popup in main window. Now the problem is when the popup is opened the text box should be auto focused.
I tried with control.Focus() and FocusManager.FocusedElement="{Binding ElementName=TextBoxName}" but no use. The popup showing curser but until I manually focus it the characters are not entering in it.
<TextBlock FontSize="16" Margin="5,0,0,3" VerticalAlignment="Center" HorizontalAlignment="Left" Text="e.g. Name"
Visibility="{Binding ElementName=TextBoxName, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBox x:Name="TextBoxName"
Text="{Binding Name}"
BorderThickness="0"
Foreground="Black"
FocusManager.FocusedElement="{Binding ElementName=TextBoxName}"
MaxLength="100"
FontSize="16"
Margin="3,1,1,1"
BorderBrush="Transparent"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
TextChanged="TextBoxName_OnTextChanged"
Height="35">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding CreateCommand}"/>
</TextBox.InputBindings>
</TextBox>
Can somebody help in this please.
Probably there is a more elegant way for this, but I used to do this with a short code behind.
I add an event handler for the TextBox's Loaded event:
In XAML:
<TextBox... Loaded="TextBox_Loaded">
Code behind:
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(delegate ()
{
Keyboard.Focus(TextBoxName);
}));
}