Touch keyboard changes layout when switching between WPF TextBox controls

Sergey Efremov 20 Reputation points
2024-04-28T10:37:17.14+00:00

Hi,

I'm experiencing a strange behaviour in a simple WPF app running on a tablet (Surface Pro 7, Windows 10.0.19043). The UI has several TextBox controls that have InputScope=Number defined.

<Window x:Class="SimpleTouchWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Margin" Value="10"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="InputScope" Value="Number"/>
        </Style>
    </Window.Resources>
    <StackPanel Orientation="Vertical">
        <TextBox/>
        <TextBox/>
        <TextBox/>
        <TextBox/>
    </StackPanel>
</Window>

On focusing the textbox, the correct touch keyboard with a numeric layout shows on the screen. Then, when entering text and making switches between text boxes, the keyboard occasionally changes from numeric to the default text one, then back to numeric on the next switch. Does not seem to happen when textboxes are empty, mostly occurs when making a switch with a simultaneous selection of part of the text.

28.04.2024_06.31.02_REC Any idea of what causes this to happen?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,786 Reputation points Microsoft Vendor
    2024-04-29T02:50:54.6533333+00:00

    Hi,@ Sergey Efremov.Welcome to Microsoft Q&A.

    The behavior you're experiencing could be due to the way Windows handles touch keyboard input and focus changes between text boxes. While the InputScope property helps to suggest the appropriate touch keyboard layout for numeric input, the actual behavior can be influenced by various factors, including the current input method editor (IME) settings, focus events, and how the touch keyboard interacts with different controls.

    Instead of setting the InputScope globally in the style, you could try setting it explicitly for each TextBox you need. This might help ensure that the touch keyboard maintains the correct layout for each TextBox consistently.

    Ensure that the focus handling between TextBox controls is smooth and consistent. If a TextBox loses focus before the touch keyboard finishes processing input, it might revert to the default text layout. You could try setting the FocusVisualStyle to null to prevent any visual indication of focus change.

    Setting this property on individual control styles and not as part of a theme is not the intended usage of this property, because it may lead to a confusing user experience regarding keyboard focus. If you are intending control-specific behavior that is deliberately not coherent across a theme, a much better approach is to use triggers in styles for individual input state properties, such as IsFocused or IsKeyboardFocusWithin, and to do so in a way that does not visually interfere with any existing focus visual style.

    Ensure that there are no event handlers or other logic in your application that might interfere with the focus handling or text input events. Sometimes, custom logic can inadvertently trigger unexpected behavior.

    The touch keyboard behavior can vary depending on the Windows version and other factors, so it might be challenging to find a consistent solution.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful