WPF - Nesting Of Layouts

Introduction

Nesting of the layout means the use of layout panel inside another layout, e.g. defining stack panels inside a grid. This concept is widely used to take the advantages of multiple layouts in an Application. In the example given below, we will be using stack panels inside a grid.

Let’s have a look at XAML code given below.

  1. <Window x:Class="WPFNestingLayouts.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFNestingLayouts" mc:Ignorable="d" Title="MainWindow" Height="350" Width="604">  
  2.     <Grid Background="AntiqueWhite">  
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="*" />  
  5.             <RowDefinition Height="*" />  
  6.             <RowDefinition Height="*" />  
  7.             <RowDefinition Height="*" />  
  8.             <RowDefinition Height="*" /> </Grid.RowDefinitions>  
  9.         <Grid.ColumnDefinitions>  
  10.             <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>  
  11.         <Label Content="Employee Info" FontSize="15" FontWeight="Bold" Grid.Column="0" Grid.Row="0" />  
  12.         <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal">  
  13.             <Label Content="Name" VerticalAlignment="Center" Width="70" />  
  14.             <TextBox Name="txtName" Text="Piyush Pansuriya" VerticalAlignment="Center" Width="200"></TextBox>  
  15.         </StackPanel>  
  16.         <StackPanel Grid.Column="0" Grid.Row="2" Orientation="Horizontal">  
  17.             <Label Content="ID" VerticalAlignment="Center" Width="70" />  
  18.             <TextBox Name="txtCity" Text="1102" VerticalAlignment="Center" Width="50"></TextBox>  
  19.         </StackPanel>  
  20.         <StackPanel Grid.Column="0" Grid.Row="3" Orientation="Horizontal">  
  21.             <Label Content="Age" VerticalAlignment="Center" Width="70" />  
  22.             <TextBox Name="txtState" Text="28" VerticalAlignment="Center" Width="50"></TextBox>  
  23.         </StackPanel>  
  24.         <StackPanel Grid.Column="0" Grid.Row="4" Orientation="Horizontal">  
  25.             <Label Content="Title" VerticalAlignment="Center" Width="70" />  
  26.             <TextBox Name="txtCountry" Text="Programmer" VerticalAlignment="Center" Width="200"></TextBox>  
  27.         </StackPanel>  
  28.     </Grid>  
  29. </Window>  

When you compile and execute the code given above, it will produce the Window given below.

 

We recommend that you execute the example code given above and try other nesting layouts.

Ebook Download
View all
Learn
View all