Silverlight / WPF - add texbox control in grid panel at a run time through for loop in wpf

Asked By sushant kumar kesari on 30-Jan-12 01:19 AM
hello frnd

i want to insert multiple text box control in grid panel through for loop .
and how to set grid.column and grid.row at a run time.

plz reply asap.
smr replied to sushant kumar kesari on 30-Jan-12 01:22 AM
D Company replied to sushant kumar kesari on 30-Jan-12 01:30 AM
Hello Shushant,

try like this. a comple code to do all the things you want. give a try to this.

private void CreateControlsUsingObjects()
{
  // <Grid Margin="10">
  Grid rootGrid = new Grid();
  rootGrid.Margin = new Thickness(10.0);
        
  // <Grid.ColumnDefinitions>
  //   <ColumnDefinition Width="100" />
  //   <ColumnDefinition Width="*" />
  //</Grid.ColumnDefinitions>
  
  rootGrid.ColumnDefinitions.Add(
    new ColumnDefinition() { Width = new GridLength(100.0) });
  rootGrid.ColumnDefinitions.Add(
    new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  
  //<Grid.RowDefinitions>
  //  <RowDefinition Height="Auto" />
  //  <RowDefinition Height="Auto" />
  //  <RowDefinition Height="Auto" />
  //  <RowDefinition Height="*" />
  //</Grid.RowDefinitions>
  
  rootGrid.RowDefinitions.Add(
    new RowDefinition() { Height = GridLength.Auto });
  rootGrid.RowDefinitions.Add(
    new RowDefinition() { Height = GridLength.Auto });
  rootGrid.RowDefinitions.Add(
    new RowDefinition() { Height = GridLength.Auto });
  rootGrid.RowDefinitions.Add(
    new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  
  //<TextBlock Text="First Name"
  //       Height="19"
  //       Margin="0,7,31,4" />
  
  var firstNameLabel = CreateTextBlock("First Name", 19, new Thickness(0, 7, 31, 4), 0, 0);
  rootGrid.Children.Add(firstNameLabel);
  
  //<TextBox x:Name="FirstName"
  //     Margin="3"
  //     Grid.Row="0"
  //     Grid.Column="1" />
  
  var firstNameField = CreateTextBox(new Thickness(3), 0, 1);
  rootGrid.Children.Add(firstNameField);
  
  //<TextBlock Text="Last Name"
  //       Margin="0,7,6,3"
  //       Grid.Row="1"
  //       Height="20" />
  
  var lastNameLabel = CreateTextBlock("Last Name", 20, new Thickness(0, 7, 6, 3), 1, 0);
  rootGrid.Children.Add(lastNameLabel);
  
        
  //<TextBox x:Name="LastName"
  //     Margin="3"
  //     Grid.Row="1"
  //     Grid.Column="1" />
  
  var lastNameField = CreateTextBox(new Thickness(3), 1, 1);
  rootGrid.Children.Add(lastNameField);
  
  
  //<TextBlock Text="Date of Birth"
  //       Grid.Row="2"
  //       Margin="0,9,0,0"
  //       Height="21" />
  
  var dobLabel = CreateTextBlock("Date of Birth", 21, new Thickness(0, 9, 0, 0), 2, 0);
  rootGrid.Children.Add(dobLabel);
  
  //<DatePicker x:Name="DateOfBirth"
  //        Margin="3"
  //        Grid.Row="2"
  //        Grid.Column="1" />
  
  DatePicker picker = new DatePicker();
  picker.Margin = new Thickness(3);
  Grid.SetRow(picker, 2);
  Grid.SetColumn(picker, 1);
  rootGrid.Children.Add(picker);
  
  //<Button x:Name="SubmitChanges"
  //      Grid.Row="3"
  //      Grid.Column="3"
  //      HorizontalAlignment="Right"
  //      VerticalAlignment="Top"
  //      Margin="3"
  //      Width="80"
  //      Height="25"
  //      Content="Save" />
  
  Button button = new Button();
  button.HorizontalAlignment = HorizontalAlignment.Right;
  button.VerticalAlignment = VerticalAlignment.Top;
  button.Margin = new Thickness(3);
  button.Width = 80;
  button.Height = 25;
  button.Content = "Save";
  Grid.SetRow(button, 3);
  Grid.SetColumn(button, 1);
  rootGrid.Children.Add(button);
  
  LayoutRoot.Children.Add(rootGrid);
}
  
private TextBlock CreateTextBlock(string text, double height, Thickness margin, int row, int column)
{
  TextBlock tb = new TextBlock() 
    { Text = text, Height = height, Margin = margin };
  Grid.SetColumn(tb, column);
  Grid.SetRow(tb, row);
  
  return tb;
}
  
private TextBox CreateTextBox(Thickness margin, int row, int column)
{
  TextBox tb = new TextBox() { Margin = margin };
  Grid.SetColumn(tb, column);
  Grid.SetRow(tb, row);
  
  return tb;
}

Regards
D
Jitendra Faye replied to sushant kumar kesari on 30-Jan-12 01:39 AM
You can create like this-

public MainPage()
{
  InitializeComponent();
 
  Loaded += new RoutedEventHandler(MainPage_Loaded);
}
 
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  CreateControls();
}
 
 
private void CreateControls()
{
  string xaml =
  "<Grid Margin='10' " +
    "<Grid.ColumnDefinitions>" +
      "<ColumnDefinition Width='100' />" +
      "<ColumnDefinition Width='*' />" +
    "</Grid.ColumnDefinitions>" +
 
    "<Grid.RowDefinitions>" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='*' />" +
    "</Grid.RowDefinitions>" +
 
    "<TextBlock Text='First Name' Height='19' Margin='0,7,31,4' />" +
    "<TextBox x:Name='FirstName' Margin='3' Grid.Row='0' Grid.Column='1' />" +
 
    "<TextBlock Text='Last Name' Margin='0,7,6,3' Grid.Row='1' Height='20' />" +
    "<TextBox x:Name='LastName' Margin='3' Grid.Row='1' Grid.Column='1' />" +
 
    "<TextBlock Text='Date of Birth' Grid.Row='2' Margin='0,9,0,0' Height='21' />" +
    "<sdk:DatePicker x:Name='DateOfBirth' Margin='3' Grid.Row='2' Grid.Column='1' />" +
 
    "<Button x:Name='SubmitChanges' Grid.Row='3' Grid.Column='3' " +
      "HorizontalAlignment='Right' VerticalAlignment='Top' " +
      "Margin='3' Width='80' Height='25' Content='Save' />" +
  "</Grid>";
 
 
  UIElement tree = (UIElement)XamlReader.Load(xaml);
 
  LayoutRoot.Children.Add(tree);
}

Try this and let me know.
Jitendra Faye replied to sushant kumar kesari on 30-Jan-12 01:40 AM
You can create like this-

public MainPage()
{
  InitializeComponent();
 
  Loaded += new RoutedEventHandler(MainPage_Loaded);
}
 
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  CreateControls();
}
 
 
private void CreateControls()
{
  string xaml =
  "<Grid Margin='10' " +
    "<Grid.ColumnDefinitions>" +
      "<ColumnDefinition Width='100' />" +
      "<ColumnDefinition Width='*' />" +
    "</Grid.ColumnDefinitions>" +
 
    "<Grid.RowDefinitions>" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='Auto' />" +
      "<RowDefinition Height='*' />" +
    "</Grid.RowDefinitions>" +
 
    "<TextBlock Text='First Name' Height='19' Margin='0,7,31,4' />" +
    "<TextBox x:Name='FirstName' Margin='3' Grid.Row='0' Grid.Column='1' />" +
 
    "<TextBlock Text='Last Name' Margin='0,7,6,3' Grid.Row='1' Height='20' />" +
    "<TextBox x:Name='LastName' Margin='3' Grid.Row='1' Grid.Column='1' />" +
 
    "<TextBlock Text='Date of Birth' Grid.Row='2' Margin='0,9,0,0' Height='21' />" +
    "<sdk:DatePicker x:Name='DateOfBirth' Margin='3' Grid.Row='2' Grid.Column='1' />" +
 
    "<Button x:Name='SubmitChanges' Grid.Row='3' Grid.Column='3' " +
      "HorizontalAlignment='Right' VerticalAlignment='Top' " +
      "Margin='3' Width='80' Height='25' Content='Save' />" +
  "</Grid>";
 
 
  UIElement tree = (UIElement)XamlReader.Load(xaml);
 
  LayoutRoot.Children.Add(tree);
}

Try this and let me know.