Unexpected Focus with AutoFocusBehavior
Opened this issue · 4 comments
Given this XAML, I expect that upon pressing Enter in the PasswordBox, the Focus should be set to the Button. However, the TextBox is focused instead.
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:behaviors="using:Cimbalino.Toolkit.Behaviors"
mc:Ignorable="d">
<interactivity:Interaction.Behaviors>
<behaviors:AutoFocusBehavior />
</interactivity:Interaction.Behaviors>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel >
<TextBox />
<PasswordBox />
</StackPanel>
<Button Grid.Row="1">CONTINUE</Button>
</Grid>
</Page>
IMHO, it would be more useful if the button was focused, since the user can save time this way and will intuitively press Enter again to "Continue"
The AutoFocusBehavior
follows the control's Control.TabIndex which if not specified, it will default to the order of the controls as they appear in the XAML (in this case TextBox
-> PasswordBox
-> Button
).
You can always specify the TabIndex
on each of the controls to get the order you want!
If all you want is to invoke a command when the Enter key is pressed on the TextBox
control, you can attach a EnterKeyBehavior to it!
Thanks for the lightning fast answer!
I would expect that pressing the Tab Key and the Enter Key would have the same behavior, while it's not true.
In the XAML I posted, if you press Tab, this is the order you'll get
- TextBox => PasswordBox => Button
However, pressing Enter will lead you to:
- TextBox => PasswordBox
I expected both to be the same
That's a fair point... when I wrote the behavior, I targeted only TextBox
and PasswordBox
, so an update will be required to get the other in!
For now, my advice is for you to copy the behavior code directly to your project and make the appropriate changes (removing the last bit on this line should suffice)
I'll do that. Thank you so much!