[英]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;
}
}
使用上面的代碼我只能綁定第一個文本框中的按鈕內容。
但我無法將值綁定在另一個文本框中。
請幫我。
編寫一個多值轉換器,它有兩個文本框作為參數,convert方法返回活動文本框的值(具有焦點)
使用您剛編寫的多值轉換器綁定按鈕內容。
這是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"/>
后端:
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.