WPF应用程序中的虚拟键盘

时间:2013-04-10 14:57:12

标签: c# wpf xaml keyboard

two text box : pseudo and password and virtual keyboard

我正在使用虚拟键盘进行WPF应用程序。如图所示,有两个文本框pseudopassword,我想使用虚拟键盘输入其值。< / p>

问题是如何知道光标位于第一个字段中或第二个字段中。我试过isfocused,但没有给出结果。

那我该怎么做呢?

public partial class Authentification : Window
{
    public TextBox numero = new TextBox();
    bool isPseudoFocused = false;
    bool isPasswordFocused = false;
    public Authentification()
    {
        InitializeComponent();
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        if (Keyboard.FocusedElement == pseudo)
            MessageBox.Show("hhhh");
    }


    private void un_Click(object sender, RoutedEvent e)
    {
        if (isPseudoFocused) pseudo.Text += "1";
        if (isPasswordFocused) password.Text += "1";
    }
    private void pseudo_FocusableChanged(Object sender, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("pseudo");
        isPseudoFocused = true;
        isPasswordFocused = false;
    }
    private void password_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("password");
        isPseudoFocused = false;
        isPasswordFocused = true;
    }
}

2 个答案:

答案 0 :(得分:2)

你应该有这样的东西:

bool isPseudoFocused = false;
bool isPasswordFocused = false;

//when Pseudo gets focus the set 
isPseudoFocused = true; 
isPasswordFocused = false;

//when Password gets focus then set
isPseudoFocused = false; 
isPasswordFocused = true;

//when you are typing text then you know where to put your text.

更新:

您应该将此代码放入TextBox_GotFocus处理程序。

private void pseudo_GotFocus(object sender, RoutedEventArgs e)
{
    MessageBox.Show("pseudo"); isPseudoFocused = true; isPasswordFocused = false;
}
private void password_GotFocus(object sender, RoutedEventArgs e)
{
    MessageBox.Show("password"); isPseudoFocused = false; isPasswordFocused = true;
}

答案 1 :(得分:1)

您可以将Keyboard.FocusedElement用于此