Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - WPF Control Layout
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

WPF Control Layout
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-08-13

    Table of Contents:
  • WPF Control Layout
  • StackPanel
  • WrapPanel
  • DockPanel
  • Grid

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    WPF Control Layout - Grid


    (Page 5 of 5 )

    The final layout control we'll be looking at is the Grid control. The Grid control behaves exactly as its name implies: it consists of a grid with rows and columns defined by the user, and each control occupies a cell within the grid. Each control has a row and a column, and a control can also have a row span and a column span in order to occupy multiple cells.

    Below, four buttons are arranged in a Grid with two rows and two columns:



    There are two steps involved in creating a Grid of controls. First, the rows and columns must be defined. Then, the controls are created. Controls can be placed in an individual cell using Grid.Row and Grid.Column attributes. The controls and layout in the above picture can be produced with the following XAML, which shows both steps necessary to create a functional Grid:


    <Grid>

     <Grid.RowDefinitions>

     <RowDefinition />

     <RowDefinition />

     </Grid.RowDefinitions>

     <Grid.ColumnDefinitions>

     <ColumnDefinition />

     <ColumnDefinition />

     </Grid.ColumnDefinitions>

     

     <Button Grid.Row="0" Grid.Column="0" Content="Button 1" />

     <Button Grid.Row="0" Grid.Column="1" Content="Button 2" />

     <Button Grid.Row="1" Grid.Column="0" Content="Button 3" />

     <Button Grid.Row="1" Grid.Column="1" Content="Button 4" />

    </Grid>


    Note that in the above example, the Grid is stretched (by default) to fit the entire Page, and the controls are stretched to fit the entire cell of the Grid. This can, of course, be changed with the HorizontalAlignment and VerticalAlignment process, but I won't bother spending time on that, since you should know the process by now. Also, the first row is the zero row, and the first column is the zero column.

    Notice how the RowDefinition and ColumnDefinition attributes have no attributes. Ordinarly, however, the rows are given a height, and the columns are given a width. Below, we make rows 30 pixels high and columns 100 pixels wide:



    <Grid>

     <Grid.RowDefinitions>

     <RowDefinition Height="30" />

     <RowDefinition />

     </Grid.RowDefinitions>

     <Grid.ColumnDefinitions>

     <ColumnDefinition Width="100" />

     <ColumnDefinition Width="100" />

     </Grid.ColumnDefinitions>

     

     <Button Grid.Row="0" Grid.Column="0" Content="Button 1" />

     <Button Grid.Row="0" Grid.Column="1" Content="Button 2" />

     <Button Grid.Row="1" Grid.Column="0" Content="Button 3" />

     <Button Grid.Row="1" Grid.Column="1" Content="Button 4" />

    </Grid>


    Instead of providing a specific measurement, however, a control can be made to take up all of the available space (or divide it up among other controls) with this setting:



    <Grid>

     <Grid.RowDefinitions>

     <RowDefinition Height="30" />

     <RowDefinition Height="30" />

     </Grid.RowDefinitions>

     <Grid.ColumnDefinitions>

     <ColumnDefinition Width="50" />

     <ColumnDefinition Width="*" />

     </Grid.ColumnDefinitions>

     

     <Button Grid.Row="0" Grid.Column="0" Content="Button 1" />

     <Button Grid.Row="0" Grid.Column="1" Content="Button 2" />

     <Button Grid.Row="1" Grid.Column="0" Content="Button 3" />

     <Button Grid.Row="1" Grid.Column="1" Content="Button 4" />

    </Grid>


    As mentioned earlier, a control can also span multiple rows:



    <Grid>

     <Grid.RowDefinitions>

     <RowDefinition Height="30" />

     <RowDefinition Height="30" />

     </Grid.RowDefinitions>

     <Grid.ColumnDefinitions>

     <ColumnDefinition Width="50" />

     <ColumnDefinition Width="50" />

     </Grid.ColumnDefinitions>

     

     <Button Grid.Row="0" Grid.RowSpan="2" Grid.Column="0"

     Content="Button 1" />

     <Button Grid.Row="0" Grid.Column="1" Content="Button 2" />

     <Button Grid.Row="1" Grid.Column="1" Content="Button 3" />

    </Grid>


    A control can also span multiple columns (the row and column definitions are the same for this example):



    <Grid>

     ...

     

     <Button Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"

     Content="Button 1" />

     <Button Grid.Row="1" Grid.Column="0" Content="Button 2" />

     <Button Grid.Row="1" Grid.Column="1" Content="Button 3" />

    </Grid>


    Now you've learned about five layout elements in WPF: Canvas, StackPanel, WrapPanel, DockPanel, and Grid. Using one of these five elements, or a combination of several of them, you can achieve some complex control arrangements that will fit any application. The only thing left is to decide what elements to apply, but I'll leave that up to you.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello, all,As was said in the article, the layout of controls plays a very large...
     

    WINDOWS SCRIPTING ARTICLES

    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...
    - Modifying XML Files in WSH
    - Reading XML Files in WSH
    - Visual Basic 2005 XML Programming Using XML ...
    - Creating an XML Document in WSH
    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT