<p>I was trying to get my head around XAML and thought that I would try writing some code.  </p>

<p>Trying to add a grid with 6 by 6 column definitions then add a text block into one of the grid cells. I don't seem to be able to reference the cell that I want. There is no method on the grid that I can add the text block too.  There is only grid.children.add(object), no Cell definition.</p>

<p>XAML:</p>

<pre><code>&lt;Page x:Class="WPF_Tester.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1"
    Loaded="Page_Loaded"&gt;

&lt;/Page&gt;
</code></pre>

<p>C#:</p>

<pre><code>private void Page_Loaded(object sender, RoutedEventArgs e)
{
    //create the structure
    Grid g = new Grid();
    g.ShowGridLines = true;
    g.Visibility = Visibility.Visible;

    //add columns
    for (int i = 0; i &lt; 6; ++i)
    {
        ColumnDefinition cd = new ColumnDefinition();
        cd.Name = "Column" + i.ToString();

        g.ColumnDefinitions.Add(cd);
    }
    //add rows
    for (int i = 0; i &lt; 6; ++i)
    {
        RowDefinition rd = new RowDefinition();
        rd.Name = "Row" + i.ToString();

        g.RowDefinitions.Add(rd);
    }
    TextBlock tb = new TextBlock();
    tb.Text = "Hello World";

    g.Children.Add(tb);
}
</code></pre>

<p><strong>Update</strong></p>

<p>Here is the spooky bit:</p>

<ul>
<li><p>Using VS2008 Pro on XP</p></li>
<li><p>WPFbrowser Project Template (3.5 verified)</p></li>
</ul>

<p>I don't get the methods in autocomplete.</p>

[{'answer_id': 7590, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 4, 'selected': True, 'text': '<p>WPF makes use of a funky thing called <a href="http://msdn.microsoft.com/en-us/library/ms749011.aspx" rel="noreferrer">attached properties</a>. So in your XAML you might write this:</p>\n\n<pre><code>&lt;TextBlock Grid.Row="0" Grid.Column="0" /&gt;\n</code></pre>\n\n<p>And this will effectively move the TextBlock into cell (0,0) of your grid.</p>\n\n<p>In code this looks a little strange. I believe it\'d be something like:</p>\n\n<pre><code>g.Children.Add(tb);\nGrid.SetRow(tb, 0);\nGrid.SetColumn(tb, 0);\n</code></pre>\n\n<p>Have a look at that link above - attached properties make things really easy to do in XAML perhaps at the expense of intuitive-looking code.</p>\n'}, {'answer_id': 7591, 'author': 'Zooba', 'author_id': 891, 'author_profile': 'https://Stackoverflow.com/users/891', 'pm_score': 0, 'selected': False, 'text': '<p>The cell location is an attached property - the value belongs to the TextBlock rather than Grid. However, since the property itself belongs to Grid, you need to use either the property definition field or the provided static functions.</p>\n\n<pre><code>TextBlock tb = new TextBlock();\n//\n// Locate tb in the second row, third column.\n// Row and column indices are zero-indexed, so this\n// equates to row 1, column 2.\n//\nGrid.SetRow(tb, 1);\nGrid.SetColumn(tb, 2);\n</code></pre>\n'}, {'answer_id': 7593, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 0, 'selected': False, 'text': '<p>Use attached properties of the Grid class.</p>\n\n<p>in C#:    </p>\n\n<pre><code>Grid.SetRow( cell, rownumber )</code></pre>\n\n<p>In XAML:</p>\n\n<pre><code>&lt;TextBlock Grid.Row="1" /&gt;</code></pre>\n\n<p>Also, I would advice if you do not use dynamic grids, use the XAML markup language. I know, it has a learning curve, but once you mastered it, it is so much easier, especially if you are going to use ControlTemplates and DataTemplates! ;)</p>\n'}, {'answer_id': 8346266, 'author': 'NoWar', 'author_id': 196919, 'author_profile': 'https://Stackoverflow.com/users/196919', 'pm_score': 0, 'selected': False, 'text': '<p>Here is some sample</p>\n\n<pre><code>Grid grid = new Grid();\n\n// Set the column and row definitions\ngrid.ColumnDefinitions.Add(new ColumnDefinition() {\n     Width = new GridLength(1, GridUnitType.Auto) });\ngrid.ColumnDefinitions.Add(new ColumnDefinition() {\n     Width = new GridLength(1, GridUnitType.Star) });\ngrid.RowDefinitions.Add(new RowDefinition() {\n     Height = new GridLength(1, GridUnitType.Auto) });\ngrid.RowDefinitions.Add(new RowDefinition() {\n     Height = new GridLength(1, GridUnitType.Auto) });\n\n// Row 0\nTextBlock tbFirstNameLabel = new TextBlock() { Text = "First Name: "};\nTextBlock tbFirstName = new TextBlock() { Text = "John"};\n\ngrid.Children.Add(tbFirstNameLabel ); // Add to the grid\nGrid.SetRow(tbFirstNameLabel , 0); // Specify row for previous grid addition\nGrid.SetColumn(tbFirstNameLabel , 0); // Specity column for previous grid addition\n\ngrid.Children.Add(tbFirstName ); // Add to the grid\nGrid.SetRow(tbFirstName , 0);  // Specify row for previous grid addition\nGrid.SetColumn(tbFirstName , 1); // Specity column for previous grid addition\n\n// Row 1\nTextBlock tbLastNameLabel = new TextBlock() { Text = "Last Name: "};\nTextBlock tbLastName = new TextBlock() { Text = "Smith"};\n\ngrid.Children.Add(tbLastNameLabel ); // Add to the grid\nGrid.SetRow(tbLastNameLabel , 1);  // Specify row for previous grid addition\nGrid.SetColumn(tbLastNameLabel , 0); // Specity column for previous grid addition\n\ngrid.Children.Add(tbLastName ); // Add to the grid\nGrid.SetRow(tbLastName , 1);  // Specify row for previous grid addition\nGrid.SetColumn(tbLastName , 1); // Specity column for previous grid addition\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/7586', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']