Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
ListBox Class
Other versions are also available for the following:
.NET Framework Class Library for Silverlight
ListBox Class
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Contains a list of selectable items.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Visual Basic (Declaration)
<TemplatePartAttribute(Name := "ScrollViewer", Type := GetType(ScrollViewer))> _
Public Class ListBox _
    Inherits ItemsControl
Visual Basic (Usage)
Dim instance As ListBox
C#
[TemplatePartAttribute(Name = "ScrollViewer", Type = typeof(ScrollViewer))]
public class ListBox : ItemsControl
Visual C++
[TemplatePartAttribute(Name = L"ScrollViewer", Type = typeof(ScrollViewer))]
public ref class ListBox : public ItemsControl
JScript
public class ListBox extends ItemsControl
XAML Object Element Usage
<ListBox .../>

The ListBox is a control that contains a collection of items. More than one item in a ListBox is visible.

Content Model:  ListBox is an ItemsControl. Its content properties are Items and ItemsSource.

The following code example creates a page with several ListBox controls configured in a variety of ways.

XAML
<UserControl x:Class="ListBoxSnippets.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="370">

    <StackPanel x:Name="LayoutRoot" Background="White" Margin="10,10,10,10">

        <TextBlock Text="ListBox Demonstration" Margin="0,20,10,20"
            FontFamily="Verdana" FontSize="18" FontWeight="Bold"
            Foreground="#FF5C9AC9" />

        <TextBlock Text="ListBox with unbound data:" />
        <ListBox Width="350" Margin="0,5,0,10">
            <TextBlock Text="TextBlock" />
            <TextBox Text="TextBox" />
            <Button Content="Button"  />                
            <Rectangle Fill="LightBlue" Height="20" Width="150"  Margin="2,2,2,2"/>
            <Ellipse Fill="Coral" Height="20" Width="150"  Margin="2,2,2,2"/>
        </ListBox>

        <TextBlock Text="ListBox with bound data:" />
        <ListBox x:Name="listBox1" Width="350" Margin="0,5,0,10" 
             DisplayMemberPath="LastName"/>

        <TextBlock Text="ListBox with ItemTemplate:" />        
        <ListBox x:Name="listBox2" Width="350" Margin="0,5,0,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0"
                            Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <TextBlock Text="ListBox with SelectionChanged event handler:" />
        <ListBox Width="350" Margin="0,5,0,5" SelectionChanged="PrintText" >
            <ListBoxItem Content="Item 1" />
            <ListBoxItem Content="Item 2" />
            <ListBoxItem Content="Item 3" />
            <ListBoxItem Content="Item 4" />
            <ListBoxItem Content="Item 5" />
        </ListBox>
        <TextBlock Name="textBlock1" />

    </StackPanel>
</UserControl>

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace ListBoxSnippets
{
    public partial class Page : UserControl
    {
        public Page()
        {
            // Required to initialize variables
            InitializeComponent();
            listBox1.ItemsSource = Customer.GetSampleCustomerList();
            listBox2.ItemsSource = Customer.GetSampleCustomerList();
        }

        void PrintText(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
            textBlock1.Text = "   You selected " + lbi.Content.ToString() + ".";
        }

    }

    public class Customer
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Address { get; set; }

        public Customer(String firstName, String lastName, String address)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Address = address;
        }

        public static List<Customer> GetSampleCustomerList()
        {
            return new List<Customer>(new Customer[4] {
                new Customer("A.", "Zero", 
                    "12 North Third Street, Apartment 45"), 
                new Customer("B.", "One", 
                    "34 West Fifth Street, Apartment 67"),
                new Customer("C.", "Two", 
                    "56 East Seventh Street, Apartment 89"),
                new Customer("D.", "Three", 
                    "78 South Ninth Street, Apartment 10")
            });
        }
    }

}

Run this sample.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker