Layout in XAML
page 3 of 8
by Keyvan Nayyeri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 19736/ 192

StackPanel

StackPanel is the simplest Panel element in XAML.  It lays out your elements in a manner similar to a stack.

StackPanel has an Orientation attribute that represents the orientation of it.  This attribute can get two values: Horizontal and Vertical.  Default is Vertical.

StackPanel shows your elements respectively according to the order you declared them in XAML file.

Listing 2 and 3 represent a Vertical and Horizontal StackPanel, respectively, that contain three TextBoxes (I used background colors to help you find the position of TextBoxes easier) and Figure 2 and 3 are their output.

Listing 2

<Window x:Class="LayoutinXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout in XAML" Height="150" Width="300"
    >
  <StackPanel>
    <TextBox Background="Coral">
      ASP Alliance 1
    </TextBox>
    <TextBox Background="Plum">
      ASP Alliance 2
    </TextBox>
    <TextBox Background="Aquamarine">
      ASP Alliance 3
    </TextBox>
  </StackPanel>
</Window>

Figure 2

Listing 3

<Window x:Class="LayoutinXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout in XAML" Height="150" Width="300"
    >
  <StackPanel Orientation="Horizontal">
    <TextBox Background="Coral">
      ASP Alliance 1
    </TextBox>
    <TextBox Background="Plum">
      ASP Alliance 2
    </TextBox>
    <TextBox Background="Aquamarine">
      ASP Alliance 3
    </TextBox>
  </StackPanel>
</Window>

Figure 3

Note that the width and height of a StackPanel governs the width and height of its children.  This means if you set a large width or height for your elements that exceeds the width and height of a StackPanel, they will be cut.  Listing 4 and Figure 4 show this in action.

Listing 4

<Window x:Class="LayoutinXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout in XAML" Height="150" Width="300"
    >
  <StackPanel Width="200">
    <TextBox Background="Coral" Width="300">
      ASP Alliance 1
    </TextBox>
    <TextBox Background="Plum">
      ASP Alliance 2
    </TextBox>
    <TextBox Background="Aquamarine">
      ASP Alliance 3
    </TextBox>
  </StackPanel>
</Window>

Figure 4


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 3 and 1 and type the answer here:

User Comments

Title: XAML Layout   
Name: Anjan
Date: 5/15/2007 6:53:25 AM
Comment:
It is fine with windows.I want to Layout the Page with WINDOWS VISTA based.
Title: Events in Windows Presentation Foundation   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:22:47 AM
Comment:
Fifth part of these tutorials about Events in Windows Presentation Foundation:
http://aspalliance.com/1088_Events_in_Windows_Presentation_Foundation
Title: Correction   
Name: Keyvan Nayyeri
Date: 10/27/2006 8:36:05 AM
Comment:
Actually nothing :-D

I just forgot to remove them from previous code but they won't affect the result anyways.

Thanks for pointing.
Title: grid...   
Name: Amirhossein Babaeian
Date: 10/27/2006 6:46:47 AM
Comment:
what's the roll of grid.row and grid.column in controls that contains in cansvas tags?
Title: Animations in XAML   
Name: Keyvan Nayyeri
Date: 10/25/2006 12:36:20 AM
Comment:
Fourth part of these tutorials about Animations in XAML:
http://aspalliance.com/1050_Animations_in_XAML
Title: Resources in XAML   
Name: Keyvan Nayyeri
Date: 10/12/2006 12:58:22 AM
Comment:
Third part of these tutorials about Resources in XAML:
http://aspalliance.com/1032_Resources_in_XAML
Title: Nice article   
Name: Harry
Date: 10/10/2006 12:27:22 AM
Comment:
Hi Keyvan,
This is very nice article about XAML. I read your previous article also, even that was alos good for begginers.

Keep it up....

Harry
Title: Introduction to XAML   
Name: Keyvan Nayyeri
Date: 10/9/2006 2:08:59 PM
Comment:
First part of these tutorials, Introduction to XAML:
http://aspalliance.com/1019_Introduction_to_XAML

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/7/2009 11:15:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search