1

两个文本框:伪密码和虚拟键盘

我正在使用虚拟键盘做一个 WPF 应用程序。如图中提到的,有两个文本框 pseudopassword我想使用虚拟键盘输入它们的值。

问题是如何知道光标是在第一个字段中还是在第二个字段中或外面。我试过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;
    }
}
4

2 回答 2

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;
}
于 2013-04-10T15:19:10.737 回答
1

你可以用Keyboard.FocusedElement这个

于 2013-04-10T15:01:57.580 回答