GridSplitter in WPF

A GridSplitter is a divider that divides a Grid into two sections. A GridSplitter allows us to resize rows or columns in a Grid by dragging the GridSplitter Bar. An example of a GridSplitter is the Windows Explorer.
 
 
 
Let's Begin:
 
Adding a Vertical GridSplitter to a Grid

In this example, I have added three columns. Out of the three Columns, I have added a GridSplitter in the second column. Here, I set the width of second column to auto so that the second column is sized automatically to fit the GridSplitter. I am using the entire second column for placing the GridSplitter that is the best way for placing the GridSplitter in a Grid. We can also place the Splitter within the column containing some content. For creating a vertical Splitter, set the VerticalAlignment property to stretch and HorizontalAlignment property of GridSplitter to Center (to create a Horizontal Splitter set the VerticalAlignment property to Center and HorizontalAlignment property of GridSplitter to stretch). Set the width and background color property of the GridSplitter so that the GridSplitter is easily visible.
  1. <Grid>  
  2.         <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition Width="150"></ColumnDefinition>  
  4.             <ColumnDefinition Width="Auto"></ColumnDefinition>  
  5.             <ColumnDefinition></ColumnDefinition>  
  6.         </Grid.ColumnDefinitions>  
  7.         <StackPanel Grid.Column="0">  
  8.             <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>  
  9.             <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>  
  10.             <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>  
  11.             <TextBlock Text="Some Text here" FontSize="16" Margin="10" TextWrapping="Wrap"></TextBlock>  
  12.         </StackPanel>  
  13.         <TextBlock Text="Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here.Some Text here" Grid.Column="2" TextWrapping="Wrap" FontSize="16" Margin="10"></TextBlock>  
  14.         <GridSplitter HorizontalAlignment="Center"   
  15.                       VerticalAlignment="Stretch"   
  16.                       Grid.Column="1"   
  17.                       Width="5" Background="Silver">  
  18.         </GridSplitter>  
  19. </Grid>  
Preview

 
 
GridSplitters in Nested Grid

For showing this, I have added 3 columns to the Grid and placed a GridSplitter in the second Column. In the first column, I added another Grid with three Rows and placed a GridSplitter in the second row.
  1. <Grid>  
  2.         <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition></ColumnDefinition>  
  4.             <ColumnDefinition Width="auto"></ColumnDefinition>  
  5.             <ColumnDefinition></ColumnDefinition>  
  6.         </Grid.ColumnDefinitions>  
  7.         <Grid.RowDefinitions>  
  8.             <RowDefinition></RowDefinition>  
  9.         </Grid.RowDefinitions>  
  10.         <!--Creating Sub-Grid in Column 0-->  
  11.         <Grid Grid.Column="0">  
  12.             <Grid.RowDefinitions>  
  13.                 <RowDefinition></RowDefinition>  
  14.                 <RowDefinition Height="auto"></RowDefinition>  
  15.                 <RowDefinition></RowDefinition>  
  16.             </Grid.RowDefinitions>  
  17.             <Button Content="Button1" Grid.Row="0"></Button>  
  18.             <Button Content="Button2" Grid.Row="2"></Button>  
  19.             <GridSplitter HorizontalAlignment="Stretch"   
  20.                           VerticalAlignment="Center"   
  21.                           Grid.Row="1" Height="4" Background="Green">  
  22.             </GridSplitter>  
  23.         </Grid>  
  24.         <!--Creating Sub-Grid in Column 2-->  
  25.         <Grid Grid.Column="2">  
  26.             <Grid.RowDefinitions>  
  27.                 <RowDefinition></RowDefinition>  
  28.                 <RowDefinition></RowDefinition>  
  29.             </Grid.RowDefinitions>  
  30.             <Button Content="Button3" Grid.Row="0"></Button>  
  31.             <Button Content="Button4" Grid.Row="1"></Button>  
  32.         </Grid>  
  33.         <GridSplitter HorizontalAlignment="Center"   
  34.                       VerticalAlignment="Stretch"   
  35.                       Grid.Column="1" Grid.Row="0"   
  36.                       Grid.RowSpan="3" Width="4" Background="Black">  
  37.         </GridSplitter>  
  38. </Grid>  
Preview

 
 
ShowsPreview property of GridSplitter

When we set the ShowsPreview property to true, a preview of the change in row or column size is shown and the size of a row or column changes when the GridSplitter is released. On setting the ShowPreview property to false, the size of the row and column updates in real time on the dragging of the GridSplitter.
  1. <Grid>  
  2.         <Grid.ColumnDefinitions>  
  3.             <ColumnDefinition></ColumnDefinition>  
  4.             <ColumnDefinition Width="auto"></ColumnDefinition>  
  5.             <ColumnDefinition></ColumnDefinition>  
  6.         </Grid.ColumnDefinitions>  
  7.         <TextBlock Text="Some Text Here.Some Text Here.Some Text Here.Some Text Here." FontSize="16" TextWrapping="Wrap" Grid.Column="0" Margin="10"></TextBlock>  
  8.         <TextBlock Text="Some Text Here.Some Text Here.Some Text Here.Some Text Here." FontSize="16" TextWrapping="Wrap" Grid.Column="2" Margin="10"></TextBlock>  
  9.         <GridSplitter HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="4" Background="Gray" Grid.Column="1" ShowsPreview="True"></GridSplitter>  
  10. </Grid>  
Preview: 
 
I hope you like it. Thanks. 
 
Other Related Articles

Up Next
    Ebook Download
    View all
    Learn
    View all