Page view counter

The Wrap Panel

 

The Silverlight Toolkit includes a wrap panel that allows you to add elements to it and will automatically wrap those elements either horizontally or vertically as required to fit them within the size of the panel. All you need to play with WrapPanel is a source of numerous controls – and the easiest way to do that is to generate them programmatically.

To do this, I'll borrow the code I used to create words in this blog entry on obtaining a lot of words, and put it to use here.   To keep the example for this blog simple, I've stripped this down, and added all the words into the source code, eliminating the code surrounding acquiring the words themselves. 

The WrapPanel does not wrap words, however, it wraps controls.  Thus, we'll use the words as source for the creation of TextBlock controls and it is the TextBlocks that we'll wrap. But to show that we can also wrap other controls (and to take a quick tangent into the programmatic creation of the source property for images) we'll add one image.

Here's how we do it.

First, we'll lay out our page in Blend.

WrapPanel

You can see that we have two rows and two columns. The picture is cropped so it is a bit difficult to see the true proportions, but the top:bottom is approximately 2:7 and left:right is approximately 1:9. 

The Go button gets the words and puts them into the word display panel much as in the previous columns, and then it calls PopulateWrapPChildren, which is the method we're interested in. Here's the result:

WrapPanelWorkingHorizontal

Clicking the Veridical Radio Button causes the controls to be reorganized within the pattern to wrap vertically rather than horizontally

WrapPanelWorkingVert

Implementation

We implement this by iterating through all the words in the collection and creating a new TextBlock for each, adding each newly created TextBlock to the WrapPanel. 

 

foreach ( string word in words )
{
  WrapP.Children.Add ( new TextBlock 
     { Text = word, Margin = new Thickness (3) } );

To make this just a tiny bit more interesting, I've added a counter, and when we've added the 10th TextBlock, I then add an Image control.  The image control needs its source set, which is trivial to do in Xaml but somewhat more complex in C#. I'll set the source to a jpeg on the Silverlight site, which can be done in a single line of code (if you have C programmer roots),

WrapP.Children.Add( new Image() {  Source = new System.Windows.Media.Imaging.BitmapImage(
  new System.Uri("http://silverlight.net/Themes/silverlight/images/learn/tutorial-icons-controls.jpg")),  
  Stretch = System.Windows.Media.Stretch.None} );

but which is far easier to understand if implemented in three easy-to-debug statements, using interim variables,

System.Uri theUri = 
   new System.Uri( "http://silverlight.net/Themes/silverlight/images/learn/tutorial-icons-controls.jpg" );

System.Windows.Media.Imaging.BitmapImage bmi = 
  new System.Windows.Media.Imaging.BitmapImage( theUri );

WrapP.Children.Add( 
   new Image 
      { 
         Source = bmi, 
         Stretch = System.Windows.Media.Stretch.None 
      } );
Here is the source: WrapPanelDemo.zip
 
 

                                                              Next: Silverlight Toolkit WrapPanel


This work is licensed under a Creative Commons Attribution By license.
Published Wednesday, December 03, 2008 12:22 PM by jesseliberty
Filed under: ,

Comments

# re: The Wrap Panel

Almost forgot! Special thanks to Justin Blunk for his assistance with the initial code!

Wednesday, December 03, 2008 12:28 PM by jesseliberty

# re: The Wrap Panel

Jesse,

if one were to use a wrap panel as a itemspaneltemplate on a listbox, and for the datatemplate use a custom control for a datatemplate on the listbox, then bind the ItemsSource to a collection of business objects. How would one get at the specific controls to trap on any events?

I've tried using the VisualTreeHelper, but after driving six layers in I still just get at the business object bound to the datatemplate object.

Thanks.

Wednesday, December 03, 2008 1:53 PM by BigDubb

# re: The Wrap Panel

BigDubb,

The fastest way to get a question like yours answered is on our spiffy new dedicated support forum for the Silverlight Toolkit: silverlight.net/.../35.aspx

Wednesday, December 03, 2008 3:55 PM by jesseliberty

# Dew Drop - December 4, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - December 4, 2008 | Alvin Ashcraft's Morning Dew

Thursday, December 04, 2008 9:06 AM by Dew Drop - December 4, 2008 | Alvin Ashcraft's Morning Dew

# Silverlight Cream for December 04, 2008 -- #445

In this issue: Jobi, Russell Greenspan(5), Mike Ormond, Tim Heuer, Matthias Shapiro(2), Terence Tsang

Thursday, December 04, 2008 9:41 PM by Community Blogs

# 2008 December 08 - Links for today « My (almost) Daily Links

Pingback from  2008 December 08 - Links for today « My (almost) Daily Links

# Silverlight Toolkit WrapPanel - Jesse Liberty - Silverlight Geek

Pingback from  Silverlight Toolkit WrapPanel - Jesse Liberty - Silverlight Geek

# Silverlight Toolkit WrapPanel

More: Previous article on the Wrap Panel The Silverlight Toolkit includes a couple new panels, one of

Monday, January 05, 2009 3:21 AM by Microsoft Weblogs

# re: The Wrap Panel

Do you agree that the ScrollViewer around the TextBlock called WordDisplay hides the latter's words a bit? You can see it in the first line and the eleventh ('sti' instead of 'still').

Tuesday, March 03, 2009 4:03 PM by marcschluper

# re: The Wrap Panel

good bind the ItemsSource to a collection of businessto

Monday, October 12, 2009 9:48 PM by www.tube.alqaly.com