The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

Some fun with the Silverlight ListBox

Given the following XAML:

<UserControl x:Class="SilverlightApplication1.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="UserControl_Loaded" 
    Width="400" Height="300"> 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <ListBox x:Name="DemoList"></ListBox> 
    </Grid> 
</UserControl>

 

with the following code:

Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        For Each number In Enumerable.Range(1, 100)
            DemoList.Items.Add(number)
        Next
    End Sub
End Class
We end up with the following Silverlight application.

image

 

No big deal but suppose we want to have the list scroll horizontally instead of vertically?

Turns out this is quite easy but, at least to me, not all that obvious. The problem is that we need to update the ItemsPanelTemplate and Blend doesn’t seem to let me do that. However using Visual Studio its a breeze.

All we need to do is change the XAML a bit like this. The bold lines are the newly added ones.

<UserControl x:Class="SilverlightApplication1.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="UserControl_Loaded" 
    Width="400" Height="300"> 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <ListBox x:Name="DemoList"> 
            <ListBox.ItemsPanel> 
                <ItemsPanelTemplate> 
                    <StackPanel Orientation="Horizontal" /> 
                </ItemsPanelTemplate> 
            </ListBox.ItemsPanel> 
        </ListBox> 
    </Grid> 
</UserControl>

And we get the following layout:

image

 

Easy right Smile

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Published Mon, Nov 17 2008 16:09 by Maurice
Filed under: , , ,

Comments

# Thailand Trip bangkok, phuket, patong beach@ Tuesday, November 18, 2008 7:03 AM

Pingback from  Thailand Trip bangkok, phuket, patong beach

# Some more fun with a Silverlight ListBox@ Tuesday, November 18, 2008 7:50 AM

In my previous post I created a horizontally scrolling ListBox. Nice but sometimes I want to have the

# re: Some fun with the Silverlight ListBox@ Sunday, November 23, 2008 3:07 PM

Instead of a for loop to populate your listbox, why not use data binding?

DemoList.ItemsSource = Enumerable.Range(0, 100);

by Joe Chung