3

how to set focus on this when i log in to my application ,when log in window will open i want to type the password on this without using mouse

<PasswordBox x:Name="passwordbox"  Grid.Row="1" Grid.Column="1"  Margin="5,35,5,5" Width="280"  Height="27" app:PasswordBoxAssistant.BindPassword="true" app:PasswordBoxAssistant.BoundPassword="{Binding TechUserModel.UserId,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" VerticalAlignment="Center"> 
Taterhead
  • 5,763
  • 4
  • 31
  • 40
ankit
  • 41
  • 8
  • 1
    `passwordbox.Focus()` on window loaded – Gopichandar Apr 04 '16 at 11:34
  • 2
    Possible duplicate of [Set focus on textbox in WPF](http://stackoverflow.com/questions/1345391/set-focus-on-textbox-in-wpf) – Phiter Apr 04 '16 at 11:34
  • i want to directly type when application become start using this code i get focus on that textbox but i can not type my password without setting cursor using mouse on that so i want directly type my password .... – ankit Apr 04 '16 at 11:45
  • Just tested from scratch with many controls and a PasswordBox. it is working with FocusManager.FocusedElement="{Binding }" on the parent container. The PasswordBox is focused and you can type. Check your styles (?!) – synapse Apr 04 '16 at 11:51

2 Answers2

2

I have tried various solutions for this scenario and the most effective that I have found is using FocusManager.FocusedElement:

<Window x:Class="StackOverflow.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="300" Width="300"
        FocusManager.FocusedElement="{Binding ElementName=Box}">
    <Grid>
        <TextBox x:Name="Box"/>
    </Grid>
</Window>
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
0

SOLUTION:

passwordbox.Focus();

or in .xaml file include the following in the element as its attributes.

FocusManager.FocusedElement="{Binding ElementName=passwordbox}">

NOTE:

If you want to decide which control to give focus to when the form first appears, put the code in the Loaded event.

Loaded += (o, e) => {
  passwordbox.Focus();
};
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43