简体   繁体   中英

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)

Bind the button content using multi value converter you just wrote.

This is the implementation in 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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