SIGN UP MEMBER LOGIN:    
ARTICLE

WrapPanel in WPF

Posted by Dinesh Beniwal Articles | WPF February 05, 2009
A WrapPanel control is used to arrange child controls to arrange vertically or horizontally. This article demonstrates how to create and use a WrapPanel control in WPF using XAML and C#.
Reader Level:
Download Files:
 

WrapPanel in WPF

A WrapPanel control is used to arrange child controls to arrange vertically or horizontally. This article demonstrates how to create and use a WrapPanel control in WPF using XAML and C#. 

Creating a WrapPanel

The WrapPanel element represents a Silverlight WrapPanel control in XAML.

 

< WrapPanel ></ WrapPanel >

 

The Orientation property is used to wrap child items horizontally or vertically.

 

The code snippet in Listing 1 creates a WrapPanel control and sets its orientation to horizontal. The output looks like Figure 5 where all child controls are wrapped horizontally.

 

<Grid x:Name="LayoutRoot" Background="White" >

    <WrapPanel Orientation="Horizontal">

            <Ellipse Width="100" Height="100" Fill="Red" />

            <Ellipse Width="80" Height="80" Fill="Orange" />

            <Ellipse Width="60" Height="60" Fill="Yellow" />

            <Ellipse Width="40" Height="40" Fill="Green" />

            <Ellipse Width="20" Height="20" Fill="Blue" />         

    </WrapPanel>

</Grid>

Listing 1

The output looks like Figure 5.

Figure 1

Now if you change orientation to vertical like Listing 2, the output looks like Figure 2.   

<WrapPanel Orientation="Vertical">

 

 Listing 2

As you can see Figure 6, the new controls are aligned vertically.

Figure 2

WrapPanel.xaml is :

<Window x:Class="HelloWPF.Window1"

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

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

    Title="Wrap Panel" Height="400" Width="300" AllowsTransparency="False" WindowStyle="ThreeDBorderWindow" Background="BlanchedAlmond" FlowDirection="LeftToRight" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">

    <Grid x:Name="LayoutRoot" Background="White" >

        <WrapPanel Orientation="Vertical">

            <Ellipse Width="100" Height="100" Fill="Red" />

            <Ellipse Width="80" Height="80" Fill="Orange" />

            <Ellipse Width="60" Height="60" Fill="Yellow" />

            <Ellipse Width="40" Height="40" Fill="Green" />

            <Ellipse Width="20" Height="20" Fill="Blue" />

            <Rectangle Width="100"  Height="100" Fill="Cyan" Cursor="None" FlowDirection="RightToLeft"></Rectangle>

            <Rectangle Width="80" Height="80" Fill="DarkGoldenrod"></Rectangle>

            <Rectangle Width="60" Height="60" Fill="Chartreuse"></Rectangle>

            <Rectangle Width="40" Height="40" Fill="Coral"></Rectangle>

            <Rectangle Width="20" Height="20" Fill="DarkGoldenrod"></Rectangle>

       

        </WrapPanel>

    </Grid>

</Window>

Output is :

Figure 3

Summary

In this article, I discussed how we can create a WrapPanel control in WPF and C#. 

 

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Become a Sponsor