WPF how do I create a textbox dynamically and find the textbox on a button click?

后端 未结 5 880
不思量自难忘°
不思量自难忘° 2021-02-04 04:19

I am creating a TextBox and a Button dynamically using the following code:

Button btnClickMe = new Button();
btnClickMe.Content = \"Cli         


        
5条回答
  •  情歌与酒
    2021-02-04 04:46

    Another method is to set the associated TextBox as Button Tag when instanciating them.

    btnClickMe.Tag = txtNumber;
    

    This way you can retrieve it back in event handler.

    protected void ClickMeClick(object sender, RoutedEventArgs e)
    {
        Button btnClickMe = sender as Button;
        if (btnClickMe != null)
        {
            TextBox txtNumber = btnClickMe.Tag as TextBox;
            // ...
        }
    }
    

提交回复
热议问题