TC Power

TC Power

  • NA
  • 6
  • 0

WPF - Creating Controls Dynamically on a Stack Panel

Sep 4 2008 9:29 PM
I am trying to create labels and textboxes dynamically on a stack panel and I can't display them at run time. The reason I am creating them at run time is I have report names and report parameters stored in a table. When I select a report from a treeview I would like to create the corresponding texboxes and labels based on the data I get from the database. Below is the code for creating Report Parameter fields. In Win forms, I could use pnlParameter.Control.Add(lbl). private void CreateRptParmsFields(int ReportNameID) { // reinitialize indexes _currCtrl = _staticCtrlCount + 1; BUI.ReportParameterCollection rpc = BUI.ReportParameterCollection.FetchListByReportNameID(ReportNameID); foreach (BUI.ReportParameter rp in rpc) { Label lbl = CreateLabelControl(rp); lbl.Tag = rp; Control ctrl; switch (rp.Control.ToLower()) { case "cmb": ctrl = CreateLabelControl(rp); break; default: ctrl = CreateTextBoxControl(rp); break; } //StackPanel p = new StackPanel(); //stpParameters = p.Children.Add(lbl).ToString(); //stpParameters = p.Children.Add(ctrl).ToString(); //pnlParams.Content = lbl; //pnlParams.Content = ctrl; ////stpParams..Controls.Add(lbl); ////stpParams.Controls.Add(ctrl); } } The two methds below are for creating labels and textboxes respectively. private TextBox CreateTextBoxControl(BUI.ReportParameter rp) { TextBox t = new TextBox(); t.Name = "txt" + rp.Code.Replace("@", ""); return t; } private Label CreateLabelControl(BUI.ReportParameter rp) { Label lbl = new Label(); lbl.Name = "lbl" + rp.Code.Replace("@", ""); lbl.Width = 150; lbl.Content = rp.Description; return lbl; } Can anyone show me how to display those fields on a stack panel. Thanks

Answers (2)