WP7 WrapPanel in depth
published on: 12/9/2010 | Views: N/A | Tags: WP7Toolkit
by WindowsPhoneGeek
In this article I will talk about the WrapPanel control which comes with the Silverlight for Windows Phone 7 Toolkit.
One of the new controls in the Silverlight Toolkit is WrapPanel. It enables users to position child elements sequentially from left to right or top to bottom. When elements extend beyond the panel edge, they are positioned in the next row or column.
To begin using WrapPanel first add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly which is installed with the toolkit and you can find it in :
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Nov10\Bin\Microsoft.Phone.Controls.Toolkit.dll.
You will also need to add the "toolkit" prefix declaration. Make sure that your page declaration includes the "toolkit" namespace:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Visual Structure
Note: A WrapPanel contains a collection of UIElement objects, which are in the Children property!
In the most common scenario WrapPanel as the name says will wrap controls, any controls which cannot fit on the first line/column will be wrapped to next line/column and so forth.
Key Properties
- ItemHeight
ItemHeight is a dependency property of type double. Gets or sets the height of the layout area for each item that is contained in a WrapPanel. The default value is Double.NaN.
- ItemWidth
ItemWidth is a dependency property of type double.Gets or sets the width of the layout area for each item that is contained in a WrapPanel. The default value is Double.NaN.
- Orientation
Orientation is a dependency property of type Orientation. Gets or sets the direction in which child elements are arranged. The default is System.Windows.Controls.Orientation.Horizontal.
Sample Usage
Here is a simple example of how to use this Panel in Horizontal and Vertical Orientation mode. We will arrange 5 items i n a WrapPanel. The code is as follows:
<TextBlock Text="Horizontal Panel"/>
<toolkit:WrapPanel ItemHeight="100" ItemWidth="100" Height="300">
<Rectangle Fill="Aqua" Height="80" Width="80"/>
<Rectangle Fill="Pink" Height="80" Width="80"/>
<Rectangle Fill="Green" Height="80" Width="80"/>
<Rectangle Fill="YellowGreen" Height="80" Width="80"/>
<Rectangle Fill="Red" Height="80" Width="80"/>
</toolkit:WrapPanel>
<TextBlock Text="Vertical Panel"/>
<toolkit:WrapPanel ItemHeight="100" ItemWidth="100" Orientation="Vertical" Height="300">
<Rectangle Fill="WhiteSmoke" Height="80" Width="80"/>
<Rectangle Fill="White" Height="80" Width="80"/>
<Rectangle Fill="Beige" Height="80" Width="80"/>
<Rectangle Fill="Bisque" Height="80" Width="80"/>
<Rectangle Fill="Wheat" Height="80" Width="80"/>
</toolkit:WrapPanel>
The result can be seen at the following screen shots:
WrapPanel as ItemsPanel
Although WrapPanel is a simple component it can be used in advanced scenarios when you want to change the ItemsPanel of any particular items control. For example you can use it in the LongListSelector, ListPicker, ListBox and other ItemsControls in order to arrange the items in suitable way:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel ItemWidth="100" ItemHeight="50"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem Content="Item1"/>
<ListBoxItem Content="Item2"/>
<ListBoxItem Content="Item3"/>
<ListBoxItem Content="Item4"/>
<ListBoxItem Content="Item5"/>
<ListBoxItem Content="Item6"/>
</ListBox>
You can find the full source code of this panel in the Silverlight for Windows Phone 7 Toolkit source code.
That was all about the Toolkit`s WrapPanel. in depth. I hope that the article was helpful.
The full source code can be downloaded here:
You can also follow us on Twitter @winphonegeek
Comments
Wrap Panel with ListBox
posted by: Dick Heuser on 2/3/2011 11:02:52 AM
I have been unable to get the Wrap Panel to work with a Listbox. I have the following XAML but it does not wrap even though each entry is far less than half of a column width. Any idea why.
RE:Wrap Panel with ListBox
posted by: winphonegeek on 2/7/2011 7:35:21 PM
I am not sure if i understand you correctly.
If you mean wrapping the items then you need to set the WrapPanel as ItemsPanel:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox>
Wrap Panel with ListBox
posted by: Dick Heuser on 2/8/2011 8:37:41 AM
Thanks. That works great.
WrapPanel with LongListSelector
posted by: P. Piper on 4/26/2011 1:25:05 PM
has anyone tried out to use the WrapPanel within the LongListSelector ?
I didn't find anything like an ItemsPanel (only the GroupedItemsPanel, which is for the "jump list")
Thank you
posted by: Dzivo on 7/3/2011 5:08:40 AM
Amazing thx ;)
RE: WrapPanel with LongListSelector
posted by: Brandon on 9/6/2011 3:13:41 PM
Ditto that, no items panel that can be set for this, somewhat disappointing.
Wrap Panel with ListBox as itemspanel
posted by: Mahantesh on 10/28/2011 8:02:46 AM
Thanks. That works great.
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
