Imagine the following simple page layout:
<Page x:Class="AutoFocusBug.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <TextBox Text="zxczczczczx"/> <Button x:Name="Button1" Content="Button1" Click="ButtonBase_OnClick"/> </StackPanel> </Page>
and the following code:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { Button1.Visibility = Visibility.Collapsed; }
When I press Button1 , it hides (which is expected and required), but for some reason the TextBox automatically gets focus and the on-screen keyboard appears. I do not want this unexpected autofocus.
I can try to do something with focus in the Click handler, but in a real application it is implemented using ViewModel with commands, etc., and it looks like a dirty hack (and also the keyboard appears for a short time, even if I change immediately focus after hiding the button).
The second approach I found was to create an “invisible” button somewhere in front of the text box (commented by FocusHolder ), which is pretty good, but also not like the right technique.
So what is this? Is this some mechanism that I can somehow adjust to “redirect” focus from a collapsed element? Or is this a mistake? What is the correct way to prevent this unwanted autofocus?
source share