How can I control the layout of items in WPF?

WPF attempts to bring the best of rich-client technologies like Windows Forms, and layout techniques from the browser to give a high degree of flexibility when positioning elements on forms.

Rich client technologies like VB6 forms, Access Forms etc were based on absolute positioning. As a developer you specified the top and left values for a control to position it on the form. This allows for a fine degree of control, but often required a large amout of code to handle form re-sizing. This was an imperative approach to layout. Controls like Labels and Panels had no awareness of the size of the content inside them - for example if the content of a label was larger than what could be displayed in the label then the content is clipped, and not shown.

Web UI rendered in the browser typically require no re-sizing code. Instead HTML gives a declarative approach to the layout of elements. You specify a serise of containers like <div> elements (or <table>, <tr> and <td> elements if you haven't become convinced of the benefits of positional CSS-based layouts) and position those by either floating them Left or Right, or positioning them absolutely in your page. Elements like <div> are aware of the size of their contents and will stretch to accomodate their content.

Both these approaches could be equally difficult to achieve the desired layout, although the declarative approach had the advantage of requiring much less code to do so. Recently windows forms has added concepts like Docking and Anchoring, adding a more declarative approach to rich-client development. WPF continues this trend with a declarative approach to layout based on the concept of panels.

Most UI elements in WPF can only contain a single child element - for example the following Xaml code fails to compile with the following error: "The 'Button' object already has a child and cannot add 'CheckBox'. 'Button' can accept only one child."

Xaml Syntax (does not compile)
<Button>
 <TextBlock>Some Text...</TextBlock>
 <CheckBox />
 <ComboBox />
</Button>

If the button allowed these two elements to be nested inside it it would be responsible for laying them out somehow. Since there are many possible ways these items could be layed out the button would have to become a much more complicated control to accomodate them all, and would now have two "responsibilities" - allowing the user to click on it, and laying out content. Not only would the button become much more complicated but all the controls in WPF would become more complicated. Instead of taking this approach the WPF team decided to factor out the responsibility for layout to a different control - the panels. There are a number of different types of panels in WPF and each enables a different kind of layout. Panels can be nested wherever regular content can be placed, allowing a fine degree of control over the layout of items. For example to correct the problem in the example above we could nest a StackPanel inside the button, and position the elements inside the StackPanel

<Button>
 <StackPanel>
  <TextBlock>Some Text...</TextBlock>
  <CheckBox />
  <ComboBox />
 </StackPanel>
</Button>

This give the following layout:

Button with nested items (stacked)

Having briefly introduced the important role panels play in controlling layout in WPF let's look at the most commonly used panel types and their characteristics.

StackPanel
The StackPanel lays out child elements by stacking them one after the other. Elements are "stacked" in the order they appear in the Xaml file (document order in XML terms). Items can either be stacked vertically (the default) or horizontally.

Xaml Syntax for Stacking Vertically (Feb 2006 CTP):
<StackPanel>
  <TextBlock FontSize="16"
             Foreground="#58290A">
    Items inside a StackPanel</TextBlock>
  <Button>Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2">
    <TextBlock>Item 3</TextBlock>
  </Border>
</StackPanel>

Content in a stack panel (vertical)

Xaml Syntax for Stacking Horizontally (Feb 2006 CTP):
<StackPanel Orientation="Horizontal">
  <TextBlock FontSize="16"
             Foreground="#58290A">
    Items inside a StackPanel</TextBlock>
  <Button>Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2">
    <TextBlock>Item 3</TextBlock>
  </Border>
</StackPanel>

Items in a StackPanel (horizontal)

WrapPanel
The wrap panel lays out items from left to right. When a row of items has filled the horizontal space available to them the panel wraps the next item around onto the next line (in a similar way to how text is layed out).

Xaml Syntax (Feb 2006 CTP):
<WrapPanel>
  <TextBlock FontSize="16"
             Foreground="#58290A">
    Items inside a WrapPanel</TextBlock>
  <Button>Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2">
    <TextBlock>Item 3</TextBlock>
  </Border>
</WrapPanel>

Items in a WrapPanel

DockPanel
The DockPanel allows elements to be docked to the edges of the panel's container, similar to Windows Forms docking. Items are docked in the order they appear in the Xaml file (document order). The last Border element fills all the remaining space since no DockPanel.Dock attribute is specified for it.

Xaml Syntax (Feb 2006 CTP):
<DockPanel>
  <TextBlock FontSize="16" DockPanel.Dock="Top"
             Foreground="#58290A">
    Items inside a DockPanel</TextBlock>
  <Button DockPanel.Dock="Left">Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2">
    <TextBlock>Item 3</TextBlock>
  </Border>
</DockPanel>

Items in a DockPanel

Canvas
The Canvas panel is similar to the way old rich-client layout worked where you control the absolute position of things by setting a Top and Left property for them. Additionally, instead of setting the Left to position an item you can instead set the Right, or instead of setting the Top you can set the Bottom. If you specify a Left and a Right the Right value is ignored, the element does not change its size to make both these values correct, and similarly Top takes precedence over Bottom. Elements that are declared earlier in the Xaml file can appear behind elements that are declared later (if their positions over-lap).

Xaml Syntax (Feb 2006 CTP):
<Canvas>
  <TextBlock FontSize="16" Canvas.Top="10" Canvas.Left="20"
             Foreground="#58290A">
    Items inside a Canvas</TextBlock>
  <Button Canvas.Bottom="25" Canvas.Right="50">Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2"
          Canvas.Top="20" Canvas.Left="50">
    <TextBlock>Item 3</TextBlock>
  </Border>
</Canvas>

Items in a Canvas

Grid
The Grid panel is an extremely flexible panel, and can be used to achieve almost everything that can be done with the other panel controls (although often not with the same ease). The grid panel allows you to define rows and columns in the grid using Xaml, and then place controls in calls in the grid using grid-specific attributes. Elements can span multiple rows or columns. The grid will automatically make all its rows and columns the same size (based on the size of the content) but you can specify a proportional size for rows and columns using a star notation, or specify an absolute width or height. The star notation can be seen in the example code below where one of the columns is twice the width of the other column by setting the Width attribute to "2*". The example below also shows the height of one row being set to an absolute value. The differences these introduce can more readily be seen when re-sizing the form containing the grid, as the grid will expand by default to fill the space available to it. The example below also has the ShowGridLines attribute of the grid set to True to help you visualize the space each row and column is taking up. A broader range of layout tricks for the grid panel is beyond the scope of this introduction, but hopefully we'll be able to re-visit this soon in a future article.

Xaml Syntax (Feb 2006 CTP):
<Grid Margin="10" ShowGridLines="True">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="2*" />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="25" />
    <RowDefinition />
    <RowDefinition  Height="2*"/>
  </Grid.RowDefinitions>

  <TextBlock FontSize="16"
             Foreground="#58290A"
             Grid.Column="0" Grid.Row="0"
             Grid.ColumnSpan="2"

             >
    Items inside a Grid</TextBlock>
  <Button Grid.Column="0" Grid.Row="1">
    Item 2</Button>
  <Border BorderBrush="#feca00" BorderThickness="2"
          Grid.Column="1" Grid.Row="2">
    <TextBlock>Item 3</TextBlock>
  </Border>
</Grid>

Items inside a Grid

We can see from the different examples above that the different Panel controls not only influence the position of the elements they're laying out, but also their size.

Implementing a Custom Panel
The WPF layout system is not limited to the layouts that are included by default. If you need a special layout that can't easily be achieved with the built-in panels you can implement your own. WPF Program Manager Kevin Moore has implemented two custom panels - an animating tile panel and a TreeMapPanel . The source for these built with the February CTP (the most recent at the time of writing) is available here.

Categories:

posted 4/6/2006 10:21:12 PM

 

Comments:

#

Your Article

posted by Shakeel on 11/9/2007 4:25:32 AM :

This is certainly a very useful tutorial, keep up the good work.

#

Great Tutorial!

posted by Yaniv Hakim on 3/18/2008 8:55:22 PM :

at last simple explanation...

#

Simple and Thorough

posted by Hanch on 4/2/2008 5:17:18 PM :

Great. 2 min. tutorial equals to a one month MS training

#

Wish everybody could make learning so simple

posted by Neetika on 6/11/2008 1:41:42 PM :

Wish everybody could make learning so simple

#

How can I control the layout of items in WPF?

posted by Mike on 9/25/2008 3:07:58 AM :

Thank you... thank you so very much...

#

You Cleared Lot !

posted by Mathivanan on 11/27/2008 11:27:22 PM :

You cleared lot of confusion and Saved my time.. Keep posting ):

#

Nice Article

posted by Sathish on 1/15/2009 5:57:25 PM :

It is extremely helpful article for begineers' like me...

#

oh wow thanks

posted by angry wpf guy on 3/20/2009 10:38:16 PM :

:D

#

Great Article

posted by Vipul on 4/2/2009 8:56:05 PM :

Fantastic article...
Very simple and very clear

#

Good Writeup

posted by Kamal on 5/5/2009 3:28:34 PM :

simple and explanatory..

#

professional shoes

posted by nike shox shoes on 11/12/2009 10:39:52 PM :

Flat feet is a mbt shoes growing problem. A lot of professional shoes people are getting pronation in the mbt usa ankle which relates to knee pain mbt uk which relates to knee injury, which relates to the hip to the back. It all nike shox shoes relates to everything, it all starts at your feet.a sports mbt outlet and prada shoes recreation manager and former Australian Olympic volleyballer, also recommends people spend money on mbt footwear to reduce the risk of injury. http://www.airjordanshow.com/

#

Ugg Boots,

posted by Ugg Boots on 11/13/2009 11:41:01 PM :

It is no use doing what ugg bailey button you like ugg boots ; you have got to like ugg classic cardy what you do My philosophy of ugg lo pro button life is work . When work is a pleasure , life is joy ! When work is duty ,ugg knightsbridge life is slavery .Work banishes those three great evils : boredom , vice, and poverty. http://www.uggshow.co.uk/specials.html


#

china wholesale

posted by china wholesale on 11/22/2009 12:43:06 AM :

http://www.cheapgoodssale.com

#

china wholesale

posted by china wholesale on 11/22/2009 12:43:12 AM :

http://www.cheapgoodssale.com

#

china wholesale

posted by china wholesale on 11/22/2009 12:43:19 AM :

http://www.cheapgoodssale.com

#

http://www.ladyugs.com/specials.html

posted by Ugg Classic Boots on 11/26/2009 1:56:58 PM :

S11125H2 Towering genius disdains a beaten path with discount ugg boots. It seeks regions hitherto unexplored.A man may lead a horse to the water, but he cannot make it drink and walk with ugg classic tall .Victory wil not come to ugg lo pro button unless I go to ugg classic cardy.Goals determine what you are going to ,for example, buy ugg knightsbridge.http://www.ladyugs.com/specials.html

#

timberland custom boots

posted by timberland chukka boots on 12/4/2009 12:23:41 PM :

S11204I2
The son of wealthy and influential Athenian parents, Plato began his timberland chukka boots philosophical career as a student of Socrates. When the master died, Plato travelled timberland waterproof boots to Egypt and Italy, studied with students of Pythagoras, and spent timberland work boots several years advising the ruling family of Syracuse. Eventually, he returned timberland custom boots to Athens and established his own school of philosophy at the Academy.For students enrolled there, Plato tried timberland high top boots both to pass on the heritage of a Socratic style of thinking Timberland Men's Roll-Top and to guide their progress through mathematical learning to the achievement of abstract philosophical truth. http://www.officialtimberland.com/products_new.html

#

timberland high top boots

posted by timberland chukka boots on 12/5/2009 3:56:16 PM :

S11205I2
The son of wealthy and influential Athenian parents, Plato began his timberland chukka boots philosophical career as a student of Socrates. When the master died, Plato travelled timberland waterproof boots to Egypt and Italy, studied with students of Pythagoras, and spent timberland work boots several years advising the ruling family of Syracuse. Eventually, he returned timberland custom boots to Athens and established his own school of philosophy at the Academy.For students enrolled there, Plato tried timberland high top boots both to pass on the heritage of a Socratic style of thinking and to guide their progress through mathematical learning to the achievement of abstract philosophical truth. http://www.officalsale.com/specials.html

#

http://www.watchgy.com/

posted by replica rolex on 12/22/2009 1:24:58 PM :

$75 Replica <a href=http://www.watchgy.com/>Rolex">http://www.watchgy.com/>Rolex Watches</a> sale, Our site provides <a href=http://www.watchgy.com/>Rolex">http://www.watchgy.com/>Rolex replica</a>, <a href=http://www.watchgy.com/>replica Rolex</a> Watches, <a href=http://www.watchgy.com/breitling-c-17.html/>replica breitling</a>, <a href=http://www.watchgy.com/cartier-c-18.html/>replica Cartier</a>, <a href=http://www.watchgy.com/omega-c-20.html/>replica Omega</a>, <a href=http://www.watchgy.com/tag-heuer-c-24.html/>Tag Heuer Watches</a>. . .more than 100 famous <a href=http://www.watchgy.com/>rolex</a> brands"
<a href=http://www.watchgy.com/rolex-air-king-c-1.html>rolex air king</a>
<a href=http://www.watchgy.com/rolex-datejust-c-9.html>rolex datejust</a>
<a href=http://www.watchgy.com/rolex-day-date-c-7.html>rolex day date</a>
<a href=http://www.watchgy.com/rolex-daytona-c-6.html>rolex daytona</a>
<a href=http://www.watchgy.com/rolex-gmt-c-11.html>rolex gmt</a>
<a href=http://www.watchgy.com/rolex-submariner-c-8.html>rolex submariner</a>
<a href=http://www.watchgy.com/rolex-yachtmaster-c-13.html>rolex yachtmaster</a>
<a href=http://www.watchgy.com/a-lange-sohne-c-14.html>a lange sohne watches</a>
<a href=http://www.watchgy.com/alain-silberstein-c-23.html>alain silberstein watches</a>
<a href=http://www.watchgy.com/bell-ross-c-15.html>bell ross watches</a>
<a href=http://www.watchgy.com/breguet-c-16.html>breguet watches</a>
<a href=http://www.watchgy.com/breitling-c-17.html>breitling watches</a>
<a href=http://www.watchgy.com/bvlgari-c-25.html>bvlgari watches</a>
<a href=http://www.watchgy.com/cartier-c-18.html>cartier watches</a>
<a href=http://www.watchgy.com/omega-c-20.html>omega watches</a>
<a href=http://www.watchgy.com/patek-philippe-c-22.html>patek philippe watches</a>r

#

swf to movie converter

posted by gnrt on 12/22/2009 8:14:36 PM :

<a href="http://www.swftomovie.com/">swf to movie</a>
<a href="http://www.swftomovie.com/swf to movie converter">swf to movie converter </a>
<a href="http://www.swftomovie.com/swf-to-avi-converter.html"> swf to avi converter</a>
<a href="http://www.swftomovie.com/swf-to-flv-converter.html"> swf to flv converter</a>
<a href="http://www.swftomovie.com/swf-to-wmv-converter.html"> swf to wmv converter</a>
<a href="http://www.swftomovie.com/swf-to-mov-converter.html"> swf to mov converter</a>
<a href="http://www.swftomovie.com/swf-to-mpeg-converter.html"> swf to mpeg converter</a>
<a href="http://www.swftomovie.com/swf-to-mp3-converter.html"> swf to MP3 converter</a>
<a href="http://www.swftomovie.com/swf-to-mp4-converter.html"> swf to MP4 converter</a>
<a href="http://www.swftomovie.com/swf-to-gif-converter.html"> swf to GIF converter</a>
<a href="http://www.swftomovie.com/free-swf-downloader.html"> Free swf downloader</a>
<a href="http://www.swftomovie.com/flv-to-avi-converter.html"> flv to avi converter</a>
<a href="http://www.swftomovie.com/flv-to-wmv-converter.html"> flv to wmv converter</a>
<a href="http://www.swftomovie.com/flv-to-mp4-converter.html"> flv to MP4 converter</a>
<a href="http://www.swftomovie.com/flv-to-mp3-converter.html"> flv to MP3 converter</a>
<a href="http://www.swftomovie.com/flv-to-video-converter.html"> flv to video converter</a>
<a href="http://www.swftomovie.com/flv-to-mov-converter.html"> flv to mov converter</a>
<a href="http://www.swftomovie.com/flv-to-mpeg-converter.html"> flv to mpeg converter</a>
<a href="http://www.swftomovie.com/web-video-downloader.html"> web video downloader</a>

#

swf to movie converter

posted by gnrt on 12/22/2009 8:18:11 PM :

http://www.swftomovie.com swf to movie
http://www.swftomovie.com/swf to movie converter swf to movie converter </a>
http://www.swftomovie.com/swf-to-avi-converter.html swf to avi converter</a>
http://www.swftomovie.com/swf-to-flv-converter.html swf to flv converter</a>
http://www.swftomovie.com/swf-to-wmv-converter.html swf to wmv converter</a>
http://www.swftomovie.com/swf-to-mov-converter.html swf to mov converter</a>
http://www.swftomovie.com/swf-to-mpeg-converter.html swf to mpeg converter</a>
http://www.swftomovie.com/swf-to-mp3-converter.html swf to MP3 converter</a>
http://www.swftomovie.com/swf-to-mp4-converter.html swf to MP4 converter</a>
http://www.swftomovie.com/swf-to-gif-converter.html swf to GIF converter</a>
http://www.swftomovie.com/free-swf-downloader.html Free swf downloader</a>
http://www.swftomovie.com/flv-to-avi-converter.html flv to avi converter</a>
http://www.swftomovie.com/flv-to-wmv-converter.html flv to wmv converter</a>
http://www.swftomovie.com/flv-to-mp4-converter.html flv to MP4 converter</a>
http://www.swftomovie.com/flv-to-mp3-converter.html flv to MP3 converter</a>
http://www.swftomovie.com/flv-to-video-converter.html flv to video converter</a>
http://www.swftomovie.com/flv-to-mov-converter.html flv to mov converter</a>
http://www.swftomovie.com/flv-to-mpeg-converter.html flv to mpeg converter</a>
http://www.swftomovie.com/web-video-downloader.html web video downloader</a>

#

Cardy Ugg Boots

posted by Cardy Ugg Boots on 1/8/2010 4:29:01 PM :

slLAH100108 I think this blog is good,so now I wanna introduce mbt to you.{{[MBT Chapa shoes->http://www.cutembt.com/mbt-chapa-shoes-c-71.html]}} are a growing and popular anti-shoe inspired by the Masai people. The Masai people are known for their athletic posture and gait. It is known that none of the Masai people struggle with any back pain due to walking on hard surfaces,so they try {{[MBT Women M.Walk shoes->http://www.cutembt.com/mbt-women-mwalk-shoes-c-42.html]}}. At last,they found the benefit of MBT Women M.Walk shoes, they trust mbt shoes.

#

mbt shoes,cheap mbt shoes,mbt shoes sale,mbt sale, mbt review

posted by mbt walking shoes on 1/12/2010 2:22:49 AM :

100109SLZLP We should so live and labor in our mbt walking shoes time that mbt usa what came to us as seed may go to the next mbt sandals generation as blossom,and what mbt sneakers came to us as blossom may go to them as fruit. This is what mbt footwear clearance we mean by progress. It is better to die on your feet than to live on your knees. http://www.mbttoday.com/

#

uggs on sale

posted by ugg boots on 1/26/2010 11:34:56 AM :

100126SLLYH
Our greatest glory uggs boots consists not in never falling but in rising every ugg classic tall time we fall.It is not enough ugg classic boots to be industrious, so are the ants. What are you industrious about ugg classic argyle knit? You have to believe in yourself. That's the ugg bailey button secret of sucess. A wise man never loses anything if he has himself. To be without some of the things you want is an indispensable part of happiness. http://www.4ezlive.com/


#

ugg cardy sela

posted by ugg cardy on 1/28/2010 12:51:25 PM :

Discount Ugg Boots On www.souggs.com
ugg cardy http://www.souggs.com/ugg-classic-cardy-c-2.html">http://www.souggs.com/ugg-classic-cardy-c-2.html
ugg boot http://www.souggs.com/
UGG Bailey Button http://www.souggs.com/ugg-bailey-button-c-24.html
UGG Classic Cardy http://www.souggs.com/ugg-classic-cardy-c-2.html">http://www.souggs.com/ugg-classic-cardy-c-2.html
UGG Classic Tall http://www.souggs.com/ugg-classic-tall-c-3.html
UGG Classic Short http://www.souggs.com/ugg-classic-short-c-1.html
UGG 5116 Mayfaire http://www.souggs.com/ugg-5116-mayfaire-c-29.html
UGG Classic Crochet http://www.souggs.com/ugg-classic-crochet-c-9.html
UGG Classic Mini http://www.souggs.com/ugg-classic-mini-c-6.html
UGG Nightfall http://www.souggs.com/ugg-nightfall-c-4.html
UGG Rainier http://www.souggs.com/ugg-rainier-c-22.html
uggs boots http://www.souggs.com/specials.html
uggs http://www.souggs.com/products_all.html
ugg australia http://www.souggs.com/page_2.html
ugg boots http://www.souggs.com/contact_us.html
cardy boots http://www.souggs.com/shippinginfo.html

#

cheap Jimmy Choo Shoes

posted by cheap Jimmy Choo Shoes on 2/1/2010 11:44:33 AM :

http://www.dearwatches.com/cartier-c-22.html Cartier watch for sale
http://www.dearwatches.com/franck-muller-c-32.html Franck Muller watch for sale
http://www.dearwatches.com/gucci-c-36.html Gucci watch for sale
http://www.dearwatches.com/iwc-c-40.html IWC watch for sale

 

Name:
 
Url (optional):
 
Subject:
 
Comment:
 

Enter Captcha Validation:
(If you cannot read the Captcha image, press "Reset Image" to generate a new one)