Span The Cells : Grid Row Column « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


public class SpanTheCells : Window
{
    [STAThread]
    public static void Main()
    {
        Application app = new Application();
        app.Run(new SpanTheCells());
    }
    public SpanTheCells()
    {
        SizeToContent = SizeToContent.WidthAndHeight;

        Grid grid = new Grid();
        grid.Margin = new Thickness(5);
        Content = grid;

        for (int i = 0; i < 6; i++)
        {
            RowDefinition rowdef = new RowDefinition();
            rowdef.Height = GridLength.Auto;
            grid.RowDefinitions.Add(rowdef);
        }
        for (int i = 0; i < 4; i++)
        {
            ColumnDefinition coldef = new ColumnDefinition();
            if (i == 1)
                coldef.Width = new GridLength(100, GridUnitType.Star);
            else
                coldef.Width = GridLength.Auto;

            grid.ColumnDefinitions.Add(coldef);
        }

        // Create labels and text boxes.
        string[] astrLabel = { "First name:" };

        for (int i = 0; i < astrLabel.Length; i++)
        {
            Label lbl = new Label();
            lbl.Content = astrLabel[i];
            lbl.VerticalContentAlignment = VerticalAlignment.Center;
            grid.Children.Add(lbl);
            Grid.SetRow(lbl, i);
            Grid.SetColumn(lbl, 0);

            TextBox txtbox = new TextBox();
            txtbox.Margin = new Thickness(5);
            grid.Children.Add(txtbox);
            Grid.SetRow(txtbox, i);
            Grid.SetColumn(txtbox, 1);
            Grid.SetColumnSpan(txtbox, 3);
        }

        // Create buttons.
        Button btn = new Button();
        btn.Content = "Submit";
        btn.Margin = new Thickness(5);
        btn.IsDefault = true;
        btn.Click += delegate { Close(); };
        grid.Children.Add(btn);
        Grid.SetRow(btn, 5);
        Grid.SetColumn(btn, 2);

        btn = new Button();
        btn.Content = "Cancel";
        btn.Margin = new Thickness(5);
        btn.IsCancel = true;
        btn.Click += delegate { Close(); };
        grid.Children.Add(btn);
        Grid.SetRow(btn, 5);
        Grid.SetColumn(btn, 3);

        // Set focus to first text box.
        grid.Children[1].Focus();
    }
}








24.43.Grid Row Column
24.43.1.ColumnDefinition.Width=AutoColumnDefinition.Width=Auto
24.43.2.ColumnDefinition.Width=*ColumnDefinition.Width=*
24.43.3.Bind to Grid Width and HeightBind to Grid Width and Height
24.43.4.Grid with Column Definition and Row definitionGrid with Column Definition and Row definition
24.43.5.Define rows / columns for GridDefine rows / columns for Grid
24.43.6.Using Grid.ColumnSpanUsing Grid.ColumnSpan
24.43.7.Set Grid Row Height and Column Width to AutoSet Grid Row Height and Column Width to Auto
24.43.8.Mixing row height stylesMixing row height styles
24.43.9.Set Column and Row index when adding Controls to a GridSet Column and Row index when adding Controls to a Grid
24.43.10.Empty RowDefinition and ColumnDefinitionEmpty RowDefinition and ColumnDefinition
24.43.11.Automatic Width and HeightAutomatic Width and Height
24.43.12.Span The Cells
24.43.13.Split Grid
24.43.14.Add controls to a Grid with loop
24.43.15.Set Grid Margin
24.43.16.Using Grid to layout all controls
24.43.17.Add control to specific row and column