Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » .NET 3.0 » A look on Layout in XAML

A look on Layout in XAML

In designing UI, layout is very important. In XAML in designing UI we have differenet type of layout such as Grid, StackPannel, Canvas, DockPannel etc.

Author Rank :
Page Views : 11492
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Layout is the basic in designing UI. XAML is specially designed for making good UI. Here in this article I am going to show some Layout in XAML. How we use these Layouts in designing our UI ? Layout and Pannel classes play an important role here in XAML to design a good UI.

Height and Width are two most important factor of any element. The element which is derived from FrameWorkElement class has two attributes. We can set the size of the elements by using height and width.

<Window x:Class="WindowsApplication1.Window1
"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation
"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
"
Title="WindowsApplication1" Height="300" Width="300
"
>
 <
Grid Name="Grid1"
>
   <
Button Click="makebutton" Name="ButtonShow" Height="100" Width="100">Click here</Button
>
 </
Grid
>
</
Window
>

The window will look like this:



Figure 1: Here we set the height and width of this button.

Now move to Pannel Classes and other in XAML. By using them we can design our UI more interactive.

  1. Grid
  2. Canvas
  3. StackPannel
  4. DockPannel

Grid  Grid is same like as Table in CSS. In a Grid there are Rows and Column. Here in Grid there is Grid.RowDefination and Grid.ColumnDefination. We use these two while working with Grid. In a Grid every child erlement can have their own Grid. We can use RowSpan and ColSpan also in a Grid.

Example:

<Window x:Class="XAMLLayout.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="XAMLLayout" Height="300" Width="300"

    >

  <Grid ShowGridLines="True">

    <!--From here we can set that how many Row we are going to Use in A Grid -->

    <Grid.RowDefinitions>

      <RowDefinition />

      <RowDefinition />

    </Grid.RowDefinitions>

 

    <!--From here we can set that how many column we are going to use in a Grid-->

    <Grid.ColumnDefinitions>

      <ColumnDefinition  />

      <ColumnDefinition />

    </Grid.ColumnDefinitions>

   

    <!-- We can use Grid.Row and Grid.column to set our element-->

    <Button Background="Blue" Grid.Row="0" Grid.Column="0" ToolTip="www.dotnetheaven.com" 

            Margin="5"> www.dotnetheaven.com</Button>

 

    <Button Background="Gray" Grid.Row="0" Grid.Column="1" ToolTip="www.dotnetheaven.com"

             Margin="5"> www.dotnetheaven.com</Button>

   

    <Button Background="Aqua" Grid.Row="1" Grid.Column="0" ToolTip="www.dotnetheaven.com"

             Margin="5"> www.dotnetheaven.com</Button>

 

    <Button Background="DarkGoldenrod" Grid.Row="1" Grid.Column="1" ToolTip="www.dotnetheaven.com"

             Margin="5"> www.dotnetheaven.com</Button>

   

  </Grid>

</Window>

The window will look like this:



Figure 2: Grid Show.

Canvas Being of Four properties(Canvas.Top, Canvas.Left, Canvas.Right and Canvas.Bottom). Canvas can be used for absolue positioning in XAML. We can set any element in a canvas whereever we want by using these 4 properties. Let see this in a  example:

 

<Window x:Class="XAMLLayout.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="XAMLLayout" Height="300" Width="300"

    >

  <Canvas>

    <Label Canvas.Left="20" Canvas.Top="20" Background="Gray"  >Name</Label>

    <TextBox Canvas.Left="80" Canvas.Top="20" Background="Beige">Rahul</TextBox>

    <Label Canvas.Bottom="35" Canvas.Right="120" Background="AntiqueWhite">www.dotnetheaven.com</Label>

    <Label Canvas.Bottom="80" Canvas.Left="200" Canvas.Right="10" Background="Brown" >.NET</Label>

   </Canvas>

</Window>

The window will become look like this:

 

Figure 3: Canvas Show.

StackPannel StackPanel is the simplest Panel element in XAML. StackPanel shows the elements respectively according to the order. In StackPannel there is orientation attribute which has two values: horizontal and vertical. By default orientation is vertical. Let see how Stack pannel works with our element:

<Window x:Class="XAMLLayout.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="XAMLLayout" Height="300" Width="300"

    >

  <!--By default this is vertical-->

  <StackPanel>

    <Label Background="AliceBlue" Width="400">Welcome</Label>    

    <TextBox Background="SaddleBrown">How r u </TextBox>

    <TextBox Background="DarkSalmon"> Good </TextBox>

  </StackPanel>

</Window>

The window will look like this:




Figure 4: StackPannel in Vertical.

Suppose if we want to show our element Horizontally aligned then:



Figure 5: StackPannel in Horizontal orientation.

DockPannel Basically both StackPannel and DockPannel are same but in DockPannel we can dock the elements top, left, right and bottom. See DockPannel by a example:

 

<Window x:Class="XAMLLayout.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="XAMLLayout" Height="300" Width="300"

    >  

  <DockPanel>

    <Label Background="AliceBlue" DockPanel.Dock="Top">Welcome</Label>

    <TextBox Background="SaddleBrown" DockPanel.Dock="Bottom">How r u </TextBox>

    <TextBox Background="DarkSalmon" DockPanel.Dock="Left"> Good </TextBox>

    <TextBox Background="Cyan" DockPanel.Dock="Right"> India </TextBox>

  </DockPanel>

</Window>

The window will look lie this:



Figure 6: DockPannel Presentation.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.