WP7 ListBox: answers to popular questions
published on: 2/4/2011 | Views: N/A | Tags: ListBox
by WindowsPhoneGeek
Today we received several questions about how to use ListBox in WP7. In this mini tutorial we will give our answers:
NOTE: First let me mention that you can take a look at the official MSDN documentation for reference.
Question 1: How to populate ListBox in WP7 in a simple way?
Answer: ListBox is a kind of ItemsControl which you can populate it with data in various ways. Basically you can either populate the ListBox control directly using ListBoxItems, or bind it to a collection of items and use the ItemsSource property. Note that is you want your collection to update its items automatically (add/remove/insert etc. ) a good choice is the ObservableCollection<T>:
“ObservableCollection<T> represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.”
Question 2: How to define ItemsPanel and what is ItemsPanel?
Answer: The ItemsPanel is the the panel used to layout the items in an ItemsControl, and can be anything that derives from the Panel class, even custom panels that you write.The default ListBox template is VirtualizingStackPanel.
“To affect the layout of the items in an ListBox, you use this property to specify a ItemsPanelTemplate.”
Take a look at the below example for reference.
Question 3: Can you give us an example of how to data bind images to my ListBox?
Answer: Take a look at the below example.
Question 4: Can I use external image Uri for my Image?
Answer: You can use external uris but do not forget to set the UriKind.Absolute if you set the image in code behind:
this.logo.Source = new BitmapImage(new Uri(@"http://www.windowsphonegeek.com/upload/appwall/wallimage.png",UriKind.Absolute));
Question 5: How can I use a WrapPanel for the ListBox items layout ?
Answer: You can use the WrapPanel from the Silverlight for WindowsPhone7 toolkit. We have explained it WrapPanel in depth in this post:
Here is the final example.
Example:
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="logo" Stretch="None"/>
<TextBlock Text="DataBound ListBox"/>
<ListBox x:Name="list">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<Image Source="{Binding ImageUri}" Stretch="None"/>
<TextBlock Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
I added several images to the project.The code behind looks like:

public class SampleData
{
public string Text
{
get;
set;
}
public string ImageUri
{
get;
set;
}
}
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
ObservableCollection<SampleData> dataSource = new ObservableCollection<SampleData>();
dataSource.Add(new SampleData() { ImageUri = "Images/appbar.close.rest.png", Text = "CLose" });
dataSource.Add(new SampleData() { ImageUri = "Images/appbar.delete.rest.png", Text = "Delete" });
dataSource.Add(new SampleData() { ImageUri = "Images/appbar.download.rest.png", Text = "Download" });
dataSource.Add(new SampleData() { ImageUri = @"http://www.windowsphonegeek.com/upload/appwall/wallimage.png", Text = "Logo" });
dataSource.Add(new SampleData() { ImageUri = @"http://www.windowsphonegeek.com/upload/appwall/wallimage.png", Text = "Logo" });
this.list.ItemsSource = dataSource;
this.logo.Source = new BitmapImage(new Uri(@"http://www.windowsphonegeek.com/upload/appwall/wallimage.png",UriKind.Absolute));
}
}

I hope that this mini tutorial was helpful. Here is the full source code.
You can also follow us on Twitter @winphonegeek
Comments
Answered!
posted by: SP on 2/4/2011 5:45:18 PM
Thanks for answering my question.
source code
posted by: Thanak Konter on 2/4/2011 5:46:29 PM
Thank you for providing the source code.
Thank you for your help.
posted by: Joraht Prahtan on 2/4/2011 5:59:00 PM
Thank you for your help.
about .gif
posted by: santi on 4/27/2011 11:25:05 AM
If the image is gif format, how do I bind it with Listbox?
thanks for the help
RE:about .gif
posted by: winphonegeek on 5/2/2011 9:58:09 AM
By default the Silverlight for WP7 image control does not support GIF files. However you can use some workarounds to display gif in WP7. Check out this discussion: Display GIF in a WP7 application with Silverlight
not answered:(
posted by: jev on 5/8/2011 9:32:02 PM
i bound data to a listbox but i want to update the values in it without reloading the page...
XAML
C#
void button_click(object sender, EventArgs e)
{
List
listbox1.ItemsSource = emp; }
void updatebuttonclick(object sender, EventArgs e)
{
List
listbox1.ItemsSource = emp; }
It updates when i click the update button but it only shows when i navigate to the page from another page...
boundary around an image
posted by: maxim on 6/19/2011 7:19:57 AM
How to make boundary around an image, in way that when you click on an item a boundary around it should change the color of (in the same way like the text changes its color)
Thanks
posted by: Almog on 6/21/2011 5:31:31 AM
Hi I was stuck on something and this totally helped.
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
