Use the Grid element to create a standard user interface (UI) dialog box.


   
        

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      WindowTitle="Grid Run Dialog Sample" WindowWidth="425" WindowHeight="225">
  <Grid Background="#DCDCDC" Width="425" Height="165" HorizontalAlignment="Left"
        VerticalAlignment="Top"
        ShowGridLines="True">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Image Grid.Column="0" Grid.Row="0" Source="c:image.png" />
    <TextBlock Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="0" TextWrapping="Wrap">
      text
    </TextBlock>
    <TextBlock Grid.Column="0" Grid.Row="1">Open:</TextBlock>
    <TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" />
    <Button Margin="10, 0, 10, 15" Grid.Row="3" Grid.Column="2">OK</Button>
    <Button Margin="10, 0, 10, 15" Grid.Row="3" Grid.Column="3">Cancel</Button>
    <Button Margin="10, 0, 10, 15" Grid.Row="3" Grid.Column="4">Browse ...</Button>
  </Grid>
</Page>

   
    
    
    
    
    
    
    
     


Place and size rectangles and ellipses in Grid cells


   
        


<Window x:Class="PlaceShapes"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Place Shapes" Height="300" Width="360">
  <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="1" Text="Rectagle"
      Margin="5" />
    <TextBlock Grid.Column="0" Grid.Row="2" Text="Ellipse"
      Margin="5" />
    <TextBlock Grid.Column="1" Grid.Row="0" Text="Fill"
      TextAlignment="Center" Margin="5" />
    <TextBlock Grid.Column="2" Grid.Row="0" Text="Uniform"
      TextAlignment="Center" Margin="5" />
    <TextBlock Grid.Column="3" Grid.Row="0" Text="UniformToFill"
      TextAlignment="Center" Margin="5" />
    <Rectangle Grid.Column="1" Grid.Row="1" Fill="Red"
      Stroke="Black" Stretch="Fill" Margin="5" />
    <Rectangle Grid.Column="2" Grid.Row="1" Fill="LightGray"
      Stroke="Black" Stretch="Uniform" Margin="5" />
    <Rectangle Grid.Column="3" Grid.Row="1" Fill="LightGray"
      Stroke="Black" Stretch="UniformToFill" Margin="5" />
    <Ellipse Grid.Column="1" Grid.Row="2" Fill="LightGray"
      Stroke="Black" Stretch="Fill" Margin="5" />
    <Ellipse Grid.Column="2" Grid.Row="2" Fill="LightGray"
      Stroke="Black" Stretch="Uniform" Margin="5" />
    <Ellipse Grid.Column="3" Grid.Row="2" Fill="LightGray"
      Stroke="Black" Stretch="UniformToFill" Margin="5" />
  </Grid>
</Window>

   
    
    
    
    
    
    
    
     


Use Image as the Grid background


   
        

<Window x:Class="OpacityMaskExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title=""
  Height="430" Width="300">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Margin="5">
      <Grid.Background>
        <ImageBrush ImageSource="c:image.jpg" />
      </Grid.Background>
      <Ellipse Width="200" Height="200" StrokeThickness="0"
        Fill="Yellow" Margin="20">
        <Ellipse.OpacityMask>
          <RadialGradientBrush GradientOrigin="0.5,0.5"
            Center="0.5,0.5" RadiusX="1" RadiusY="1">
            <GradientStop Offset="0" Color="Transparent" />
            <GradientStop Offset="1" Color="Yellow" />
          </RadialGradientBrush>
        </Ellipse.OpacityMask>
      </Ellipse>
    </Grid>



  </Grid>
</Window>

   
    
    
    
    
    
    
    
     


Automatic Width and Height


   
         
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid Background="Beige" ShowGridLines="True">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <TextBlock Grid.Column="0" Grid.Row="0">This is a test.:</TextBlock>
  <TextBlock Grid.Column="1" Grid.Row="0">This is a test</TextBlock>
  <TextBlock Grid.Column="0" Grid.Row="1">This is a test:</TextBlock>
  <TextBlock Grid.Column="1" Grid.Row="1">This is a test</TextBlock>
  <TextBlock Grid.Column="0" Grid.Row="2">This is a test:</TextBlock>
  <TextBlock Grid.Column="1" Grid.Row="2">This is a test</TextBlock>
</Grid>
</Page>

   
    
    
    
    
    
    
    
    
     


Grid with SharedSizeGroup


   
            

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" 
  Background="OrangeRed">

    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" IsSharedSizeScope="True">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="myGroup"/>
        <ColumnDefinition/>
        <ColumnDefinition SharedSizeGroup="myGroup"/>
      </Grid.ColumnDefinitions>
      <Label Grid.Column="0" Background="Red" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">1</Label>
      <GridSplitter Grid.Column="0" Width="5"/>
      <Label Grid.Column="1" Background="Orange" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">2</Label>
      <Label Grid.Column="2" Background="Yellow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">3</Label>
    </Grid>
</Window>

   
    
    
    
    
    
    
    
    
    
    
    
     


Layout a Form with StackPanel and Grid


   
            
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="UseExpander" Height="300" Width="300">
    <Grid>
        <StackPanel Name="StackPanel1" Margin="0,0,0,0">
            <Expander Header="Name" Margin="0,0,0,0" Name="Expander1" IsExpanded="True">
                <StackPanel Margin="20,0,0,0">
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">First Name</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">Last Name</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                </StackPanel>
            </Expander>
            <Separator />
            <Expander Header="Address" Margin="0,0,0,0" IsExpanded="True">
                <StackPanel Margin="20,0,0,0" >
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">Street</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">City</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">State</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                    <StackPanel Height="Auto" Width="Auto" Orientation="Horizontal">
                        <Label Height="25.96" Width="84">Zip</Label>
                        <TextBox Height="25" Width="147" />
                    </StackPanel>
                </StackPanel>
            </Expander>
            <Separator />
        </StackPanel>
    </Grid>
</Window>

   
    
    
    
    
    
    
    
    
    
    
    
     


Clear All Columns


   
  
<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="ColumnDefinitions Sample">
    <Border BorderBrush="Black" Background="White" BorderThickness="2">
  <DockPanel Margin="10,0,0,0">
    <TextBlock FontSize="20" FontWeight="Bold" DockPanel.Dock="Top">Grid Column and Row Collections</TextBlock>
        <Grid DockPanel.Dock="Top" HorizontalAlignment="Left" Name="grid1" ShowGridLines="true" Width="625" Height="400">
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
          <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
        </Grid>

        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
            <Button Width="125" Click="clearCol">Clear All Columns</Button>
        </StackPanel>    

  </DockPanel>
  </Border>  
</Window>

//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        RowDefinition rowDef1;
        ColumnDefinition colDef1;

        private void clearCol(object sender, RoutedEventArgs e)
        {
            grid1.ColumnDefinitions.Clear();
        }
    }
}