C# 查找活动文本框WPF

C# 查找活动文本框WPF,c#,wpf,C#,Wpf,我正在使用WPF为kiosk设备创建一个入口页面。页面和键盘中有3个文本框(使用按钮创建)。要在中按键盘按钮时执行操作,请在相应的文本框中显示文本 需要:如何找到当前关注的文本框 代码使用: void buttonElement_Click(object sender, RoutedEventArgs e) { // create variable for holding string String sendString = "";

我正在使用WPF为kiosk设备创建一个入口页面。页面和键盘中有3个文本框(使用按钮创建)。要在中按键盘按钮时执行操作,请在相应的文本框中显示文本

需要:如何找到当前关注的文本框

代码使用:

    void buttonElement_Click(object sender, RoutedEventArgs e)
    {
        // create variable for holding string
        String sendString = "";

        try
        {
            // stop all event handling
            e.Handled = true;
            Button btn = ((Button)sender);

            // set sendstring to key
            if (btn.Content.ToString().Length == 1 && btn.CommandParameter.ToString() != btn.Content.ToString())
            {
                sendString = btn.Content.ToString();
            }
            else
            {
                sendString = btn.CommandParameter.ToString();
            }

            // sendString = ((Button)sender).CommandParameter.ToString();
            int position = txtAuto.SelectionStart;

            // if something to send
            if (!String.IsNullOrEmpty(sendString))
            {
                // if sending a string
                if (sendString.Length > 1)
                {
                    switch (sendString)
                    {
                        case "Del":
                            if (position != txtAuto.Text.Length)
                            {
                                txtAuto.Text = txtAuto.Text.Remove(position, 1);
                                txtAuto.SelectionStart = position;
                            }
                            break;

                        case "BACKSPACE":
                            if (position != 0)
                            {
                                txtAuto.Text = txtAuto.Text.Remove(position - 1, 1);
                                txtAuto.SelectionStart = position;
                            }
                            break;

                        case "Clear":
                            txtAuto.Text = string.Empty;
                            break;
                        case "ENTER":
                            popup.IsOpen = false;
                            // lbSuggestion.ItemsSource = null;

                            this.FetchSearchResult(txtAuto.Text.Trim());
                            if (lbResult.Items.Count != 0)
                            {
                                lbResult.ScrollIntoView(lbResult.Items[0]);
                            }

                            break;
                    }
                }
                else
                {
                    txtAuto.Text = txtAuto.Text.Insert(txtAuto.SelectionStart, sendString);
                    txtAuto.SelectionStart = position + 1;
                }

                // set keyboard focus
                System.Windows.Input.Keyboard.Focus(this.txtAuto);
                // set normal focus
                this.txtAuto.Focus();
            }
        }
        catch (Exception)
        {
            // do nothing - not important for now
            Console.WriteLine("Could not send key press: {0}", sendString);
        }
    }

这段代码可以很好地用于单个文本框,如何使它适用于其他文本框

当您单击该按钮时,该按钮将接收焦点,而文本框将丢失焦点。因此,一种方法是订阅所有文本框的事件,并记住哪一个失去了焦点。上次失去焦点的是由于按钮单击而失去焦点的,因此是在单击之前有焦点的。当您单击按钮时,按钮接收焦点,文本框失去焦点。因此,一种方法是订阅所有文本框的事件,并记住哪一个失去了焦点。上次失去焦点的是因为点击按钮而失去焦点的,因此是在点击之前有焦点的

需要:如何找到当前关注的文本框

你可以使用这个方法

需要:如何找到当前关注的文本框


您可以使用该方法。

通常,如果单击按钮,焦点会丢失。因此,如果文本框失去焦点,可以在类变量中“保存”最后一个焦点文本框

private TextBox _currentTextbox;

private void TextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    _currentTextbox = e.Source as TextBox;
}
将此处理程序附加到所有文本框,并在函数中使用
\u currentTextbox


更多信息请参见

正常情况下,如果单击按钮,焦点会丢失。因此,如果文本框失去焦点,可以在类变量中“保存”最后一个焦点文本框

private TextBox _currentTextbox;

private void TextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    _currentTextbox = e.Source as TextBox;
}
将此处理程序附加到所有文本框,并在函数中使用
\u currentTextbox

更多信息请访问