2012-07-10 62 views
0

我正在開發使用C#和XAML的地鐵應用程序,所以我需要一些幫助將一些XAML代碼轉換爲C#,下面的代碼在XAML中,並且工作正常,我想在C#以行和列格式將數據插入網格

<Grid x:Name="ObjRootGrid" Background="Black" Margin="248,198,227,182"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="78*"/> 
     <RowDefinition Height="101*"/> 
     <RowDefinition Height="94*"/> 
     <RowDefinition Height="111*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="111*"/> 
     <ColumnDefinition Width="143*"/> 
     <ColumnDefinition Width="147*"/> 
     <ColumnDefinition Width="141*"/> 
     <ColumnDefinition Width="141*"/> 
    </Grid.ColumnDefinitions> 
    <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="61,49,0,0" Grid.Row="1" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
    <TextBlock Grid.Column="2" HorizontalAlignment="Left" Margin="71,45,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
    <TextBlock HorizontalAlignment="Left" Margin="68,45,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
    <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="106,58,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
    <TextBlock HorizontalAlignment="Left" Margin="63,58,0,0" Grid.Row="3" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 

</Grid> 

任何一個可以幫助我這個請,在此先感謝

回答

3
  var grid = new Grid() { 
       Name = "ObjRootGrid", 
       Background = new SolidColorBrush(Colors.Black), 
       Margin = new Thickness(248, 198, 227, 182) 
      }; 
      var rowDef1 = new RowDefinition(); 
      rowDef1.Height = new GridLength(78, GridUnitType.Star); 
      grid.RowDefinitions.Add(rowDef1); 
      //continue adding row definitions... 
      var colDef1 = new ColumnDefinition(); 
      colDef1.Width = new GridLength(111, GridUnitType.Star); 
      grid.ColumnDefinitions.Add(colDef1); 
      //continue adding column definitions... 

      var textBox1 = new TextBlock() { 
       HorizontalAlignment = System.Windows.HorizontalAlignment.Left, 
       Margin = new Thickness(61,49,0,0), 
       TextWrapping = TextWrapping.Wrap, 
       Text = "TextBlock", 
       VerticalAlignment = System.Windows.VerticalAlignment.Top 
      }; 
      Grid.SetColumn(textBox1, 1); 
      Grid.SetRow(textBox1, 1); 
      grid.Children.Add(textBox1); 
      //continue adding text blocks...