简体   繁体   English

绑定文本框具有光标的按钮内容

[英]bind the button content which textbox has the cursor

I tried to create On-screen keyboard. 我试图创建屏幕键盘。

在此输入图像描述

Here i want to bind the button content, which textbox has the cursor. 这里我想绑定按钮内容,哪个文本框有光标。

public partial class current_cursor : Window
{
    public current_cursor()
    {
        this.InitializeComponent();     

    }

    private void btn_a_Click(object sender, RoutedEventArgs e)
    {
        txt_diplay_1.Text += btn_a.Content;
    }

}

with above code i can only bind the button content in first textbox. 使用上面的代码我只能绑定第一个文本框中的按钮内容。

but i can't bind the value in another textbox. 但我无法将值绑定在另一个文本框中。

please help me. 请帮我。

Write a multi value converter that has two textboxes as parameters and the convert method returns the value of the active textbox (that has focus) 编写一个多值转换器,它有两个文本框作为参数,convert方法返回活动文本框的值(具有焦点)

Bind the button content using multi value converter you just wrote. 使用您刚编写的多值转换器绑定按钮内容。

This is the implementation in WPF: 这是WPF中的实现:

<TextBox Height="23" Margin="30,28,128,0" Name="textBox1" VerticalAlignment="Top" GotFocus="textBox1_GotFocus" />
<TextBox Height="23" Margin="58,86,100,0" Name="textBox2" VerticalAlignment="Top"  GotFocus="textBox2_GotFocus"/>

Backend: 后端:

 Control ctrl = null;
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (ctrl != null)
        {
            TextBox tb = ctrl as TextBox;
            tb.Text += Convert.ToString(button1.Content);
        }
    }


    private void textBox2_GotFocus(object sender, RoutedEventArgs e)
    {
        ctrl = (Control)sender;
    }

    private void textBox1_GotFocus(object sender, RoutedEventArgs e)
    {
        ctrl = (Control)sender;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM