动态插入文本框

时间:2013-12-17 05:35:39

标签: c# wpf dynamic textbox

直到现在我只需要在堆栈面板上显示一些复选框。因此我正在进行

构建复选框的内容。

 public void addAlarmcl(string argComparator, string value, string argColor)
        {
            if ((!string.IsNullOrEmpty(argComparator)) && (!string.IsNullOrEmpty(value)) && (!string.IsNullOrEmpty(argColor)))
            {
                //hexadecimal of color chosen
                Color color = (Color)ColorConverter.ConvertFromString(argColor);
                //this is to be displayed in stackpenl
                string TheOneYouJustBuilt = "If statistics are /" + argComparator + "/" + value + "/ ,notify by /" + color + "/.";
                addToStackPanel(TheOneYouJustBuilt);
            }

        }  

然后将其作为

添加到堆栈面板中
//this will display the built alarms on the stackpanel
        public void addToStackPanel(string argBuiltAlarm)
        { 
            CheckBox checkQueries = new CheckBox() { Content = argBuiltAlarm };
            stackPanel1.Children.Add(checkQueries);
            AlarmList.Add(checkQueries);
            //storing the built queries alongside FOR DELETION (OR OTHER REFERENCES)
            AlarmThatIsBeingDisplayed.Add(argBuiltAlarm.ToString());
        }

但现在我需要在复选框内容的末尾有一个文本框。如何修改我的代码以添加该文本框?

我知道要声明一个新的文本框

 var textbox = new TextBox();
 //thn we set its properties and all

但是如何在复选框内容之后附加它?

2 个答案:

答案 0 :(得分:1)

试试此代码

public void addToStackPanel(string argBuiltAlarm)
{
   //creating a stackpanel with orientation horizontal
   StackPanel stackPanel=new StackPanel
        {
           Orientation =System.Windows.Controls.Orientation.Horizontal
        };

   TextBox textBox=new TextBox{ Text =  "your text"};

   CheckBox checkQueries = new CheckBox() { Content = argBuiltAlarm };

   stackPanel.Children.Add(checkQueries);
   stackPanel.Children.Add(textBox);

   //adding the stackpanel containing both checkbox & textbox to the stackPanel1
   stackPanel1.Children.Add(stackPanel);

   //remaining codes here

 }

答案 1 :(得分:0)

您需要在一些容器中包装复选框和文本框:StackPanel或Grid:

var sp = new StackPanel() 
{
    Orientation = Orientation.Horizontal
};
sp.Children.Add(checkQueries);
sp.Children.Add(textbox);
stackPanel1.Children.Add(sp);