Question

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

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.

XAML:

<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">

</Page>

C#:

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 < 6; ++i)
    {
        ColumnDefinition cd = new ColumnDefinition();
        cd.Name = "Column" + i.ToString();

        g.ColumnDefinitions.Add(cd);
    }
    //add rows
    for (int i = 0; i < 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);
}

Update

Here is the spooky bit:

  • Using VS2008 Pro on XP

  • WPFbrowser Project Template (3.5 verified)

I don't get the methods in autocomplete.

Was it helpful?

Solution

WPF makes use of a funky thing called attached properties. So in your XAML you might write this:

<TextBlock Grid.Row="0" Grid.Column="0" />

And this will effectively move the TextBlock into cell (0,0) of your grid.

In code this looks a little strange. I believe it'd be something like:

g.Children.Add(tb);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 0);

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.

OTHER TIPS

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.

TextBlock tb = new TextBlock();
//
// Locate tb in the second row, third column.
// Row and column indices are zero-indexed, so this
// equates to row 1, column 2.
//
Grid.SetRow(tb, 1);
Grid.SetColumn(tb, 2);

Use attached properties of the Grid class.

in C#:

Grid.SetRow( cell, rownumber )

In XAML:

<TextBlock Grid.Row="1" />

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! ;)

Here is some sample

Grid grid = new Grid();

// Set the column and row definitions
grid.ColumnDefinitions.Add(new ColumnDefinition() {
     Width = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition() {
     Width = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() {
     Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition() {
     Height = new GridLength(1, GridUnitType.Auto) });

// Row 0
TextBlock tbFirstNameLabel = new TextBlock() { Text = "First Name: "};
TextBlock tbFirstName = new TextBlock() { Text = "John"};

grid.Children.Add(tbFirstNameLabel ); // Add to the grid
Grid.SetRow(tbFirstNameLabel , 0); // Specify row for previous grid addition
Grid.SetColumn(tbFirstNameLabel , 0); // Specity column for previous grid addition

grid.Children.Add(tbFirstName ); // Add to the grid
Grid.SetRow(tbFirstName , 0);  // Specify row for previous grid addition
Grid.SetColumn(tbFirstName , 1); // Specity column for previous grid addition

// Row 1
TextBlock tbLastNameLabel = new TextBlock() { Text = "Last Name: "};
TextBlock tbLastName = new TextBlock() { Text = "Smith"};

grid.Children.Add(tbLastNameLabel ); // Add to the grid
Grid.SetRow(tbLastNameLabel , 1);  // Specify row for previous grid addition
Grid.SetColumn(tbLastNameLabel , 0); // Specity column for previous grid addition

grid.Children.Add(tbLastName ); // Add to the grid
Grid.SetRow(tbLastName , 1);  // Specify row for previous grid addition
Grid.SetColumn(tbLastName , 1); // Specity column for previous grid addition
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top