Windows Scripting
  Home arrow Windows Scripting arrow 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


    (Page 1 of 5 )

    One of the most important decisions when designing a WPF application is how to lay out the application's controls. Controls need to be put in the application in a way that makes sense and makes the application easy to use. Control placement is facilitated through a number of layout elements, such as Grid and StackPanel. Using these, developers can easily lay out controls to achieve the desired look. WPF offers several layout elements designed for various purposes, and in this article, we'll take a look at each of them.

    Note that the examples in this article are all created as part of browser-hosted applications for the purpose of taking simple screenshots. Furthermore, the dimensions of the pages may be modified in order to make the result small and visually appealing, or to show off a particular feature (e.g. wrapping with the WrapPanel).

    Canvas

    We'll start by looking at the Canvas control. This control is a bit unique in that it doesn't arrange its children controls in any way. Rather, each child can be positioned individually using an absolute positioning system. So, in one light, Canvas can be seen as primitive, and in another light, it can be seen as flexible, since you're not confined to a particular layout scheme when using it. The Canvas control, then, can be used to create some fancy applications.

    Here's an example of an interesting layout that can be created with Canvas:



    You can arrange controls by specifying their Canvas.Top, Canvas.Left, Canvas.Bottom, and Canvas.Right attributes, which map to positions on the canvas. Canvas.Top specifies the distance from the top of the Canvas, Canvas.Left specifies the distance from the left side of the Canvas, Canvas.Bottom specifies the distance from the bottom of the Canvas, and Canvas.Right specifies the distance from the right side of the canvas. So, a control with a Left value of 10 and a Top value of 20 would have its top left corner 10 pixels from the left side of the Canvas and 20 pixels from the top of the canvas. The above layout, then, can be created like this:


    <Canvas>

     <Button Canvas.Left="10" Canvas.Top="10"

     Content="Button 1" />

     <Button Canvas.Left="60" Canvas.Top="60"

     Content="Button 2" />

     <Button Canvas.Left="110" Canvas.Top="110"

     Content="Button 3" />

     <Button Canvas.Left="60" Canvas.Top="160"

     Content="Button 4" />

     <Button Canvas.Left="10" Canvas.Top="210"

     Content="Button 5" />

    </Canvas>


    Above, Canvas.Left and Canvas.Top are specified. However, let's use Canvas.Right and Canvas.Bottom to create a mirror image:



    <Canvas>

     <Button Canvas.Right="10" Canvas.Bottom="210"

     Content="Button 1" />

     <Button Canvas.Right="60" Canvas.Bottom="160"

     Content="Button 2" />

     <Button Canvas.Right="110" Canvas.Bottom="110"

     Content="Button 3" />

     <Button Canvas.Right="60" Canvas.Bottom="60"

     Content="Button 4" />

     <Button Canvas.Right="10" Canvas.Bottom="10"

     Content="Button 5" />

    </Canvas>

    More Windows Scripting Articles
    More By Peyton McCullough


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

    WINDOWS SCRIPTING ARTICLES

    - 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
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





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