locked
WPF - Creating Controls Dynamically on a Stack Panel RRS feed

  • Question

  • 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


    New to C#
    Friday, September 5, 2008 1:33 AM

Answers

  • You can add the controls to the StackPanel, sample code is below

                StackPanel panel = new StackPanel();  
                panel.Orientation = Orientation.Vertical;  
                Image img = new Image();  
                img.Source = new BitmapImage(new System.Uri(@"C:\Shawshank.JPG"));  
                panel.Children.Add(img);  
                TextBlock text = new TextBlock();  
                text.Text = "My Button";  
                panel.Children.Add(text);  
                this.Children.Add(panel);
     

    However, there is are more better way to display your data. You can have a Listbox or ListView and you can define a template for your ListBoxItem or ListViewItem. The template can have your TextBox and Label controls which are bind to the data.

    You can get some idea from the link below: 
    http://www.c-sharpcorner.com/UploadFile/raj1979/DataBinding10042007165025PM/DataBinding.aspx

    You can modify the GridViewTemplate as below:

          <GridViewColumn Header="Songs" Width="100">  
              <GridViewColumn.CellTemplate> 
                <DataTemplate> 
                  <TextBox Name="textBoxSongs"  IsReadOnly="False" TextWrapping="WrapWithOverflow" Text="{Binding    
     
                     XPath=Songs}" VerticalContentAlignment="Center">  
                  </TextBox> 
                </DataTemplate> 
              </GridViewColumn.CellTemplate> 
            </GridViewColumn> 
     
    • Proposed as answer by DutchMarcel Friday, September 5, 2008 7:58 AM
    • Marked as answer by Marco Zhou Thursday, September 11, 2008 10:11 AM
    Friday, September 5, 2008 5:28 AM

All replies

  • You can add the controls to the StackPanel, sample code is below

                StackPanel panel = new StackPanel();  
                panel.Orientation = Orientation.Vertical;  
                Image img = new Image();  
                img.Source = new BitmapImage(new System.Uri(@"C:\Shawshank.JPG"));  
                panel.Children.Add(img);  
                TextBlock text = new TextBlock();  
                text.Text = "My Button";  
                panel.Children.Add(text);  
                this.Children.Add(panel);
     

    However, there is are more better way to display your data. You can have a Listbox or ListView and you can define a template for your ListBoxItem or ListViewItem. The template can have your TextBox and Label controls which are bind to the data.

    You can get some idea from the link below: 
    http://www.c-sharpcorner.com/UploadFile/raj1979/DataBinding10042007165025PM/DataBinding.aspx

    You can modify the GridViewTemplate as below:

          <GridViewColumn Header="Songs" Width="100">  
              <GridViewColumn.CellTemplate> 
                <DataTemplate> 
                  <TextBox Name="textBoxSongs"  IsReadOnly="False" TextWrapping="WrapWithOverflow" Text="{Binding    
     
                     XPath=Songs}" VerticalContentAlignment="Center">  
                  </TextBox> 
                </DataTemplate> 
              </GridViewColumn.CellTemplate> 
            </GridViewColumn> 
     
    • Proposed as answer by DutchMarcel Friday, September 5, 2008 7:58 AM
    • Marked as answer by Marco Zhou Thursday, September 11, 2008 10:11 AM
    Friday, September 5, 2008 5:28 AM
  • I would also go for a databound ItemsControl solution. If your collection is not too exotic this should work fine. ItemsControl itself is the simplest and looks most like the StackPanel I think.
    Dr. WPF is the expert on item controls so make sure to check out his blog:
    http://drwpf.com/blog/ItemsControlSeries/tabid/59/Default.aspx
    Especially the 'C' and 'D' entry should help you with your problem.

    hth,
    Marcel
    Friday, September 5, 2008 7:58 AM