c#XAML - 第二次WPF窗口仅在第二次触摸点击后响应

时间:2015-08-01 16:27:38

标签: c# wpf xaml

我有一个键盘:                                                                                             

    <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,键盘会打开我刚刚点击的位置,并且没有突出显示的按钮。如果我用鼠标滚动按钮,那么我可以触摸任何按钮,它可以立即工作。否则我必须先选择一个按钮(突出显示至少一个),现在我可以触摸任何按钮。我感觉它可能还在等待窗口上的文本字段,所以这就是为什么它只对第二次触摸做出反应。

1 个答案:

答案 0 :(得分:0)

好的,这个问题是由于我正在使用

引起的
private void userIDTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)

而不是:

private void userIDTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)

我在选择用户ID文本框时加载了我的键盘,因此我认为它一直停留在该文本框内,直到我再次选择了键盘。通过使用PreviewMouseUp加载键盘,然后它直接切换到我的键盘窗口。