Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a child to a grid, set it's row and column

How can I append an Image object into a Grid and set it's Row and Column?

The grid is 3x3.

Main file:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="440" Width="400" ResizeMode="NoResize">
    <Window.Background>
        <ImageBrush ImageSource="C:\Users\GuyD\AppData\Local\Temporary Projects\WpfApplication1\AppResources\Background.png"></ImageBrush>
    </Window.Background>
    <Grid ShowGridLines="True" x:Name="myGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="42" />
            <RowDefinition Height="30*" />
            <RowDefinition Height="30*" />
            <RowDefinition Height="32*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="31*" />
            <ColumnDefinition Width="26*" />
            <ColumnDefinition Width="32*" />
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

Code behind file:

public MainWindow()
{
     InitializeComponent();
     for (int i = 0; i < 3; i++)
     {
          for (int j = 0; j < 3; j++)
          {
               Image Box = new Image();
               this.myGrid.Children.Add(Box);
          }
     }
}
like image 939
Novak Avatar asked Jul 28 '12 14:07

Novak


People also ask

What are the child elements of a grid called?

The following example defines a parent Grid element ( grid1 ) that has three columns and three rows. A child Rectangle element ( rect1 ) is added to the Grid in column position zero, row position zero. Button elements represent methods that can be called to reposition the Rectangle element within the Grid.

What is grid RowSpan?

You can span across multiple rows and columns using the Grid. RowSpan and Grid. ColumnSpan attached properties. The default value for both these properties is 1. The Grid will attempt to assign as many row spans or column spans as it can up to the amount specified by the Grid.

What is grid concept?

A grid is a set of intersecting horizontal and vertical lines defining columns and rows. Elements can be placed onto the grid within these column and row lines.

What is Grid C#?

C# interfaces - Blazor, API, UWP, WPF, Office 30 Lectures 2.5 hours. More Detail. A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties.


2 Answers

The Grid setter methods are static.
To place them in row 1 column 1:

Image Box = new Image(); myGrid.Children.Add(Box); Grid.SetRow(Box, 1); Grid.SetColumn(Box, 1); 
like image 159
Gerard Sexton Avatar answered Sep 27 '22 23:09

Gerard Sexton


You can use following to set for any UIElement

Grid.SetRow(Box, i);
Grid.SetColumn(Box, j);
like image 43
Rohit Agrawal Avatar answered Sep 27 '22 23:09

Rohit Agrawal