Reputation: 569
I have one "login page" and one "content page". The login page consists of a PasswordBox
and a Button
. When the button is clicked or the Enter-key is pressed in the PasswordBox
, I want the program to navigate to the next page. Navigating with the button works fine, but when the page is changed due to the pressed enter key, the Button
on the next page is focused. How can I disable/avoid this behaviour?
MainPage.xaml
<Page
x:Class="TabNavigation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TabNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel>
<PasswordBox x:Name="textBox" KeyDown="HandleTextBox_KeyDown"/>
<Button x:Name="loginButton" Content="Einloggen" Click="HandleLoginButton_Click"/>
</StackPanel>
</Grid>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace TabNavigation {
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
}
private void HandleTextBox_KeyDown(object sender, KeyRoutedEventArgs e) {
if(e.Key == Windows.System.VirtualKey.Enter) {
this.Frame.Navigate(typeof(ContentPage));
}
}
private void HandleLoginButton_Click(object sender, RoutedEventArgs e) {
this.Frame.Navigate(typeof(ContentPage));
}
}
}
ContentPage.xaml
<Page
x:Class="TabNavigation.ContentPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TabNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel>
<Button>Test</Button>
</StackPanel>
</Grid>
</Page>
Upvotes: 1
Views: 36
Reputation: 7881
You can cancel the focus in GettingFocus
:
<Button GettingFocus="Button_GettingFocus">Test</Button>
private void Button_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
args.Cancel = true;
}
Old answer:
You have few options:
IsTabStop=false
TabIndex
to larger integer than other elements.otherControl.Focus(FocusState.Programmatic);
Upvotes: 1