System.Windows.Controls Nam ...


.NET Framework Class Library
ListBox Class

Updated: February 2009

Contains a list of selectable items.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic (Declaration)
<StyleTypedPropertyAttribute(Property := "ItemContainerStyle", StyleTargetType := GetType(ListBoxItem))> _
<LocalizabilityAttribute(LocalizationCategory.ListBox)> _
Public Class ListBox _
    Inherits Selector
Visual Basic (Usage)
Dim instance As ListBox
C#
[StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(ListBoxItem))]
[LocalizabilityAttribute(LocalizationCategory.ListBox)]
public class ListBox : Selector
Visual C++
[StyleTypedPropertyAttribute(Property = L"ItemContainerStyle", StyleTargetType = typeof(ListBoxItem))]
[LocalizabilityAttribute(LocalizationCategory::ListBox)]
public ref class ListBox : public Selector
JScript
public class ListBox extends Selector
XAML Object Element Usage
<ListBox>
  Items
</ListBox>
Remarks

Content Model:  ListBox is a ItemsControl. Its content properties are Items and ItemsSource. For more information on the content model for ListBox, see Controls Content Model Overview.

The ListBox is a control that contains a collection of items. More than one item in a ListBox is visible, unlike the ComboBox, which has only the selected item visible unless the IsDropDownOpen property is true. The SelectionMode property determines whether more than one item in the ListBox is selectable at a time.

The SelectionMode property determines how many items a user can select at one time. You can set the property to Single (the default), Multiple, or Extended. The following table described the behavior of these enumeration values.

Value

Description

Single

The user can select only one item at a time.

Multiple

The user can select multiple items without holding down a modifier key.

Extended

The user can select multiple consecutive items while holding down the SHIFT key or non-consecutive items by holding down the CTRL key and clicking the items.

ListBox controls are often used with data binding. For more information, see Data Binding Overview.

Displaying a large number of items may cause performance issues. See Optimizing Performance: Controls for more information.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

Examples

The following example creates a ListBox and subscribes to the SelectionChanged event.

XAML
<TextBox Name="tb" Width="140" Height="30"></TextBox>
<ListBox Name="lb" Width="100" Height="55" SelectionChanged="PrintText" SelectionMode="Single">
  <ListBoxItem>Item 1</ListBoxItem>
  <ListBoxItem>Item 2</ListBoxItem>
  <ListBoxItem>Item 3</ListBoxItem>
  <ListBoxItem>Item 4</ListBoxItem>
  <ListBoxItem>Item 5</ListBoxItem>
  <ListBoxItem>Item 6</ListBoxItem>
  <ListBoxItem>Item 7</ListBoxItem>
  <ListBoxItem>Item 8</ListBoxItem>
  <ListBoxItem>Item 9</ListBoxItem>
  <ListBoxItem>Item 10</ListBoxItem>
</ListBox>
Visual Basic
Private Sub PrintText(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

    Dim lbsender As ListBox
    Dim li As ListBoxItem

    lbsender = CType(sender, ListBox)
    li = CType(lbsender.SelectedItem, ListBoxItem)
    tb.Text = "   You selected " & li.Content.ToString & "."
End Sub
C#
void PrintText(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}
Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.Control
              System.Windows.Controls..::.ItemsControl
                System.Windows.Controls.Primitives..::.Selector
                  System.Windows.Controls..::.ListBox
                    System.Windows.Controls..::.ListView
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Change History

Date

History

Reason

February 2009

Described how default styles change dependency properties.

Customer feedback.

Tags :


Community Content

LukeSkywalker
Luke's Guide to the ListBox

Every time I go to sleep I forget how this, may I say, overly complicated beast of a control works. Here's some community content that might help programmers of a similar disposition.

Right, a ListBox is a bunch of behaviours over the top of an ItemsControl which is essentially a controller for items in a Panel, but with data binding wizardry inside. The ListBox handles some stuff like selection but the ItemsControl is the big gun.

Its default template consists of a Border, a ScrollViewer (of which the ScrollViewer has a ScrollContentPresenter) and an ItemsPresenter - which is where the Panel is drawn. Note that no specific Panel is specified in the default template but the one it gets is a VirtualizingStackPanel which I think has some cleverness to not render things that aren't visible or something.

This Panel is usually changed via ItemsControl.ItemsPanel but could also be specified in the control template by replacing the ItemsPresenter with your favourite Panel and setting its IsItemsHost=true which will hook it up to the ItemsControl/ListBox.

The ItemsControl's job is to generate the items (usually from a binding to ItemsSource) and add them to the Panel according to customizable templates using an ItemContainerGenerator. For a ListBox, this makes ListBoxItem instances which are ContentControls and thus have ContentPresenters in their templates. Their look is affected using...

ItemTemplate.

Except that it should not be used in conjunction with ItemTemplateSelector, because that accepts your very own DataTemplateSelector class which returns a DataTemplate when its SelectTemplate method is called - and so you can add some kind of logic to to this method to dish out different templates accordingly.

And we're not done yet because there's also ItemContainerStyle and ItemContainerStyleSelector which do a similar job except that they style the container (ListBoxItem) around the item's actual rendered content (as defined by ItemTemplate).

As for the look of the highlight, I guess that's done as part of the template applied to the item in reaction to ListBoxItem.IsSelected. Probably.


Page view tracker