Dynamically add textbox in WPF

前端 未结 2 478
南笙
南笙 2021-01-28 00:40

I am creating a textbox dynamically. I have 2 columns in my grid. I want to add new textbox to the row if the other textbox value=\"tea\". I want to create new textbox to corres

2条回答
  •  我在风中等你
    2021-01-28 01:41

    If you just want to achieve this, then more elegant way will be to add the user control to your application like below:

    
        
            
                
                
            
            
            
                
                    
                
            
    
        
    
    

    then in your btn_addnew_Click() method, you just need to add this usercontrol to user Grid and assign row and column to it. Showing/Hiding of textbox will be taken care of by teh user control itself.

        var userControl = new MyUserControl();
        userControl .Margin = new Thickness(10, 10, 0, 0);
        Grid.SetRow(userControl , i);
    
        grid1.Children.Add(userControl );
    

    OR if you want to have value of Grid.Row for textbox1 you can get it directly as:

            if (textbox1 != null)
            {
               int row = (int)textbox1.GetValue(Grid.RowProperty);
               Grid.SetRow(txt2, row);
            }
    

提交回复
热议问题