【问题标题】:WPF: Using a Virtual KeyboardWPF:使用虚拟键盘
【发布时间】:2018-02-21 17:15:09
【问题描述】:

我创建了一个虚拟键盘用户控件,以便在我的应用程序的多个窗口中使用。我想知道当按下一个键时如何让它输入到窗口中的文本框中。

我正在寻找的是这样的:

private void keyboardKey_Click(object sender, RoutedEventArgs e){
var key = sender as Button;
textbox.Text += key.Content;
}

例如,如果我按下 'a' 的键,那么 'a' 会添加到文本框。

我的想法倾向于某种绑定属性,但由于我是 WPF 新手,我不知道从哪里开始。像

<local:QWERTYKeyboard TextboxBinding="TextboxName"/>

谢谢

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    这是一项相当复杂的任务。幸运的是,有几个关于这个主题的好教程。

    我建议您阅读以下两个教程:

    特别是第一个应该包含一个示例应用程序,可以帮助您入门。

    关于将文本放入 TextBox 的特定问题。一种(幼稚的)实现是跟踪focus

    您的虚拟键盘可能具有包含当前焦点文本框的属性:

    public TextBox FocusedTextBox {get;set;}
    

    您应用的每个文本框都可以根据 GotFocus 事件更新属性:

    private void txtBox_GotFocus(object sender, RoutedEventArgs e)
    {
        // Set virtual keyboards' active textbox
        this.VirtualKeyboard.FocusedTextBox = txtBox;
    }
    

    现在在你的虚拟键盘中,当你按下“a”时,你可以更新文本框的内容:

    private void UserPressedVirtualKeyboard(object sender, RoutedEventArgs e)
    {
        this.VirtualKeyboard.FocusedTextBox.Text = this.VirtualKeyboard.FocusedTextBox.Text + pressedChar;
    }
    

    【讨论】:

    • 谢谢。重点工作可以完成这项工作,我不敢相信它是如此简单以至于我忽略了。再次感谢。
    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2013-01-03
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多