第二个WPF窗口只在第二次触摸点击后响应

本文关键字:响应 触摸 第二次 WPF 窗口 第二个 | 更新日期: 2023-09-27 18:06:04

我有一个这样的键盘:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
    <Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False"  Click="btn_Click"></Button>
    <Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
    <Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
    <Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btn_Click"></Button>
    <Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"/>
    <Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"/>
    <Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>
    <Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>
    <Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>
    <Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>
    <Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>
    <Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btn_Click"></Button>

当我调用这个键盘来使用用户名和密码时,用鼠标点击它可以正常工作。但是当我使用触屏显示器时,我必须先点击键盘窗口内的任何地方,然后它才会接受第一个按钮的按压。请问我该如何解决这个问题?

//click inside user ID text box
    private void userIDTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        userIDTextBox.Text = "";                            //clears text box text
        getKeyPadAndResult();                               //run helper method   
        userIDTextBox.Text = loginKeypad.QuantityResult;    //add text from keypad tologin screen

    }
    //Click inside password text box
    private void passcodeTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        passCodeTextBox.Text = "";                            //clears text box text
        getKeyPadAndResult();                               //run helper method
        passCodeTextBox.Text = loginKeypad.QuantityResult;  //add text from keypad tologin screen
     }

    //*****first helper method***** to open keypad, remove default text from login textboxes and pass back numbers
    private void getKeyPadAndResult()
    {
        loginKeypad.QuantityResult = "";                    //resets text               
        loginKeypad.Owner = this;                           //opens login keypad
        loginKeypad.ShowDialog();
        loginKeypad.Focus();
    }

编辑:所以事实证明它这样做的原因是由于鼠标在按钮上没有被启用。因为我选择了一个打开小键盘的TEXTFIELD,小键盘在我刚刚单击的地方打开,没有突出显示按钮。如果我用鼠标滚动一个按钮,那么我可以触摸任何按钮,它可以立即工作。否则,我必须先选择一个按钮(以突出显示至少一个),现在我可以触摸任何按钮。我有一种感觉,它可能还在等待窗口上的文本框,所以这就是为什么它只对第二次触摸做出反应。

第二个WPF窗口只在第二次触摸点击后响应

这个问题是由于我使用

private void userIDTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)

代替:

private void userIDTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)

当选择userID文本框时,我的键盘正在加载,所以我假设它留在文本框内,直到我再次选择键盘。通过使用PreviewMouseUp来加载键盘,然后它直接切换到我的键盘窗口。