WPF Layout: Dealing With Percentage Size in WPF

Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This series of articles explain the layout process in WPF. This series began with an understanding of the WPF layout process. The next part of this series will cover the basics of layout and positioning, such as size, margin, padding and alignment of elements. Later in this series, I will cover various panels and related parent controls available in WPF.

Table of Contents

Introduction

In the previous article, WPF Layout: Content Alignment, I explained content alignment of elements. What if you need to adjust the size of elements based on the percentage of a container or screen? This article focuses on how to deal with percentage in the size of elements.

Manage Percentage

In HTML, the percentage (%) symbol is used to define a uniform layout that keeps the same width and height ratio when a web page is resized. We do not have this feature in Windows Forms. However, WPF supports this feature in a different manner using an asterisk (*) suffix with a double number. Unlike percentage, an asterisk does not have a maximum limit of 100. An asterisk uses the current width or height of an element and divides by the value associated with the asterisk and when a Window or Page is resized, the actual size of the element is calculated at runtime.

Here is an example. Figure 1 is a Window with a Grid panel and three Rectangle elements.


                           Figure 1

You can create this UI by simply placing 3 rectangles on a Grid element and moving them to where you want. The XAML code looks as in Listing 1.

  1. <Grid>  
  2.    <Rectangle Name="rectangle1" Stroke="Black" Fill="Orange" Margin="55,0,112,80" />  
  3.    <Rectangle Fill="Green" Margin="0,80,0,0" Name="rectangle2" Stroke="Black"  
  4.       HorizontalAlignment="Right" Width="113" />  
  5.    <Rectangle Fill="Purple" Margin="0,80,0,0" Name="rectangle3" Stroke="Black"  
  6.       HorizontalAlignment="Left" Width="54" />  
  7. </Grid>  

Listing 1

Now when we resize the Window, we want the size of the rectangles to have the same ratio as the new size of the window. We can do this using an asterisk with the size.

However, all elements in XAML do not support the asterisk feature. We place columns and rows in a Grid element and fix their width and height with the asterisk.

The new code is listed in Listing 2.

  1. <Window x:Class="PercentageInWPF.Window1"  
  2.    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.    Title="Window1" Height="200" Width="300">  
  5.   
  6.    <Grid>  
  7.       <Grid.RowDefinitions>  
  8.          <RowDefinition Height="139*" />  
  9.          <RowDefinition Height="150*" />  
  10.       </Grid.RowDefinitions>  
  11.   
  12.       <Grid.ColumnDefinitions>  
  13.          <ColumnDefinition Width="101*" />  
  14.          <ColumnDefinition Width="208*" />  
  15.          <ColumnDefinition Width="169*" />  
  16.       </Grid.ColumnDefinitions>  
  17.   
  18.       <Rectangle Grid.Column="1" Name="rectangle1" Stroke="Black" Fill="Orange" />  
  19.       <Rectangle Fill="Green" Name="rectangle2" Stroke="Black" Grid.Column="2" Grid.Row="1" />  
  20.       <Rectangle Fill="Purple" Name="rectangle3" Stroke="Black" Grid.Row="1" />  
  21.    </Grid>  
  22. </Window>  

Listing 2

Now if you resize the window, the size of rectangles will be changed in proportion to the size of the window in Figure 2.


                                                                      Figure 2

Summary

In this article, I explained how to deal with the % size of WPF elements. The next article of this series explains WPF layout and an understanding of panels.

Up Next
    Ebook Download
    View all
    Learn
    View all