Home  separator  Layout  separator  StackPanel
Bookmark and Share Share...    Subscribe to this feed Feed   About me...


WPF StackPanel

Introduction

The StackPanel in WPF is a simple and useful layout panel. It stacks its child elements below or beside each other, dependening on its orientation. This is very useful to create any kinds of lists. All WPF ItemsControls like ComboBox, ListBox or Menu use a StackPanel as their internal layout panel.

 
<StackPanel>
  <TextBlock Margin="10" FontSize="20">How do you like your coffee?</TextBlock>
  <Button Margin="10">Black</Button>
  <Button Margin="10">With milk</Button>
  <Button Margin="10">Latte machiato</Button>
  <Button Margin="10">Chappuchino</Button>
</StackPanel>
 
 

Stack Items horizontally

A good example for a horizontal stack panel are the "OK" and "Cancel" buttons of a dialog window. Because the size of the text can change if the user changes the font-size or switches the language we should avoid fixed sized buttons. The stack panel aligns the two buttons depending on their desired size. If they need more space they will get it automatically. Never mess again with too small or too large buttons.

 
<StackPanel Margin="8" Orientation="Horizontal">            
   <Button MinWidth="93">OK</Button>
   <Button MinWidth="93" Margin="10,0,0,0">Cancel</Button>
</StackPanel>
 
 



 Comments on this article

Show all comments
Tormentor
Commented on 9.May 2009
Can I stretch the last element in the empty space of the stack panel without giving it any width or height? any help if possible!
Christian Moser
Commented on 10.May 2009
Hi Tormentor,
It's not possible to strech the last element of a stack panel because the stack panel has "infinite" size for its elements. You can solve your problem by using a DockPanel and dock all contained elements to top and set LastChildFill to true.

Greetings
Christian
Maurizio Longo
Commented on 9.July 2009
Thanks a lot for doing this job: it's really helpful!
My contribution could be telling you how that it is Cappuccino and Latte Macchiato the correct spelling :-)
Huy Nguyen
Commented on 24.August 2009
Thanks for useful tutorial.
I can find how to use thing in WPF quickly.
kamran
Commented on 10.September 2009
Nice stack panel overview
saru
Commented on 12.September 2009
Only this much.Can u plz provide some more information for a new user this is enough but need some more clarification
Swaroop
Commented on 13.December 2009
Simple and quick explaination helped a lot
pooja
Commented on 4.January 2010
can u telme hw to use datagridview in wpf.
asdddddddddd...
Commented on 29.January 2010
saddddddddddddd
sathya
Commented on 11.February 2010
nice one
A
Commented on 24.February 2010
Very nice articles.. Thanks!!
moti
Commented on 6.March 2010
thanks for everything, really useful articles! it seems more clear to me than before!
sam
Commented on 8.April 2010
really nice.. thanks
WomenLover43
Commented on 24.April 2010
All I know is I like's my women thick!
WomenLover43
Commented on 26.April 2010
All I know is I like's my women thick!
Joe Rossi of...
Commented on 7.June 2010
I am not sure this really helps me
Bansi
Commented on 19.June 2010
Baans maar riya hai ye example! Need more details yaar.
Hichem
Commented on 6.July 2010
we need dynamik coding for this utilities
humanblade
Commented on 11.July 2010
oh just so you know. in english 'Chappuchino' is a gay sex act in which a man wearing assless chaps shits upon the chin of his lover (chaps+poo+chin+OMG!) you had probably mean cappuccino, but just wanted to make you aware in case site started drawing the attention of chappuchino lovers.
humanblade
Commented on 11.July 2010
oh yeah, and your Akismet thing doesn't seem to be working for spam avoidance. i was wondering what was going on with all the apparently east asian named commentators.
Eric Layne
Commented on 19.July 2010
I'm guessing this article has been left unfinished? It barely begins to describe many of the properties of a stackpanel.
uÄŸur
Commented on 14.August 2010
mal bu ne la salak bu şey yapmayı bilmiyonuz zuhahaha :D
Christian Moser
Commented on 17.August 2010
nice article but need to be more elaboration
MOSER
Commented on 23.August 2010
I apprieciate your feedback but I cant explain it anymore in depth because I just copy and pasted this from another website
sm
Commented on 31.August 2010
Thanks, like to learn WPF here :)

Name
E-Mail (optional)
Comment
About Christian Moser