Programmatically use the positioning methods of Grid : Grid Operations « Windows Presentation Foundation « C# / CSharp Tutorial






<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Grid Methods Sample">
    <DockPanel VerticalAlignment="Top" HorizontalAlignment="Left">
        <Grid Margin="10" DockPanel.Dock="Left">
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Orientation="Vertical">
            <Button Click="setCol0">Move Rectangle to Column 0</Button>
            <Button Click="setCol1">Move Rectangle to Column 1</Button>
            <Button Click="setCol2">Move Rectangle to Column 2</Button>
            <Button Click="setRow0">Move Rectangle to Row 0</Button>
            <Button Click="setRow1">Move Rectangle to Row 1</Button>
            <Button Click="setRow2">Move Rectangle to Row 2</Button>
            <Button Click="setColspan">Span All Columns</Button>
            <Button Click="setRowspan">Span All Rows</Button>
          </StackPanel>
        </Grid>
      <Grid DockPanel.Dock="Top" Name="grid1" ShowGridLines="True" Width="400" Height="400">
        <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
        <Rectangle Name="rect1" Fill="Silver" Grid.Column="0" Grid.Row="0"/>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="0" Margin="5">Column 0, Row 0</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="0" Margin="5">Column 1, Row 0</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="0" Margin="5">Column 2, Row 0</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="1" Margin="5">Column 0, Row 1</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Margin="5">Column 1, Row 1</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="1" Margin="5">Column 2, Row 1</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="2" Margin="5">Column 0, Row 2</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="2" Margin="5">Column 1, Row 2</TextBlock>
        <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="2" Margin="5">Column 2, Row 2</TextBlock>
      </Grid>
    </DockPanel>
</Window>
//File:Window.xaml.cs


using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
    private void setCol0(object sender, RoutedEventArgs e) 
    {
            Grid.SetColumn(rect1, 0);
            Console.WriteLine("Column " + Grid.GetColumn(rect1).ToString());

    }
        private void setCol1(object sender, RoutedEventArgs e)
        {
            Grid.SetColumn(rect1, 1);
            Console.WriteLine("Column " + Grid.GetColumn(rect1).ToString());
        }
        private void setCol2(object sender, RoutedEventArgs e)
        {
            Grid.SetColumn(rect1, 2);
            Console.WriteLine("Column " + Grid.GetColumn(rect1).ToString());
        }
    private void setRow0(object sender, RoutedEventArgs e)
    {
            Grid.SetRow(rect1, 0);
            Console.WriteLine("Row " + Grid.GetRow(rect1).ToString());
    }
        private void setRow1(object sender, RoutedEventArgs e)
        {
            Grid.SetRow(rect1, 1);
            Console.WriteLine("Row " + Grid.GetRow(rect1).ToString());
        }
        private void setRow2(object sender, RoutedEventArgs e)
        {
            Grid.SetRow(rect1, 2);
            Console.WriteLine("Row " + Grid.GetRow(rect1).ToString());
        }
        private void setColspan(object sender, RoutedEventArgs e)
    {
            Grid.SetColumnSpan(rect1, 3);
            Console.WriteLine("ColumnSpan " + Grid.GetColumnSpan(rect1).ToString());
    }
    private void setRowspan(object sender, RoutedEventArgs e)
    {
            Grid.SetRowSpan(rect1, 3);
            Console.WriteLine("RowSpan " + Grid.GetRowSpan(rect1).ToString());
    }
        private void clearAll(object sender, RoutedEventArgs e)
        {
            Grid.SetColumn(rect1, 0);
            Grid.SetRow(rect1, 0);
            Grid.SetColumnSpan(rect1, 1);
            Grid.SetRowSpan(rect1, 1);
        }
  }
}
WPF Programmatically Use The Positioning Methods Of Grid








24.45.Grid Operations
24.45.1.Set Row Height and column Width for GridSet Row Height and column Width for Grid
24.45.2.Set Row and Column Index when Adding Buttons to GridSet Row and Column Index when Adding Buttons to Grid
24.45.3.Clear All ColumnsClear All Columns
24.45.4.Clear All RowsClear All Rows
24.45.5.Remove One ColumnRemove One Column
24.45.6.Remove One RowRemove One Row
24.45.7.The current number of ColumnsThe current number of Columns
24.45.8.The current number of RowsThe current number of Rows
24.45.9.Remove 5 Columns with ColumnDefinitions.RemoveRangeRemove 5 Columns with ColumnDefinitions.RemoveRange
24.45.10.Remove 5 Row with RowDefinitions.RemoveRangeRemove 5 Row with RowDefinitions.RemoveRange
24.45.11.Contains RowContains Row
24.45.12.Contains Column?Contains Column?
24.45.13.Insert RowInsert Row
24.45.14.Insert ColumnInsert Column
24.45.15.Is Grid ReadOnlyIs Grid ReadOnly
24.45.16.Add a ColumnDefinition to GridAdd a ColumnDefinition to Grid
24.45.17.Add a RowDefinition to GridAdd a RowDefinition to Grid
24.45.18.Setting Grid row heights in codeSetting Grid row heights in code
24.45.19.Set control to specific row and column in codeSet control to specific row and column in code
24.45.20.Show Grid lines in codeShow Grid lines in code
24.45.21.Programmatically use the positioning methods of GridProgrammatically use the positioning methods of Grid