cimbalino / Cimbalino-Toolkit

:coffee: Cimbalino Toolkit
https://cimbalino.org
MIT License
114 stars 16 forks source link

Unexpected Focus with AutoFocusBehavior #60

Open SuperJMN opened 7 years ago

SuperJMN commented 7 years ago

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"

pedrolamas commented 7 years ago

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!

SuperJMN commented 7 years ago

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

However, pressing Enter will lead you to:

I expected both to be the same

pedrolamas commented 7 years ago

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)

SuperJMN commented 7 years ago

I'll do that. Thank you so much!