System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
ListBoxItem Class

Represents a selectable item in a ListBox.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Syntax

Visual Basic (Declaration)
<TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Unselected", GroupName := "SelectionStates")> _
<TemplateVisualStateAttribute(Name := "Selected", GroupName := "SelectionStates")> _
<TemplateVisualStateAttribute(Name := "SelectedUnfocused", GroupName := "SelectionStates")> _
<TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _
Public Class ListBoxItem _
    Inherits ContentControl
Visual Basic (Usage)
Dim instance As ListBoxItem
C#
[TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Unselected", GroupName = "SelectionStates")]
[TemplateVisualStateAttribute(Name = "Selected", GroupName = "SelectionStates")]
[TemplateVisualStateAttribute(Name = "SelectedUnfocused", GroupName = "SelectionStates")]
[TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
public class ListBoxItem : ContentControl
XAML Object Element Usage
<ListBoxItem .../>
-or-
<listboxitem ...>
  content
</listboxitem>

XAML Values

content

A single object element that serves as content.

listboxitem

A ListBoxItem object element, or an object element for a class that derives from ListBoxItem.

Remarks

A ListBox contains a collection of ListBoxItem objects. To select a ListBoxItem in a ListBox, set the IsSelected property to true.

Bind a ListBoxItem to data by binding the ItemsSource property of a ListBox to a data source.

You can customize the appearance of a ListBoxItem by setting the ItemTemplate property of a ListBox to a DataTemplate.

By default, the HorizontalAlignment of a ListBoxItem is set to Stretch. The default horizontal position of a StackPanel is Center. If you set the Width property of a ListBoxItem through a StackPanel, the panel's default is applied and the item is centered. ListBoxItem is a ContentControl. Its content property is Content.

Customizing the ListBoxItem Control

To apply the same property settings to multiple ListBoxItem controls, use the Style property. Alternatively, you can set the ItemContainerStyle property of the parent ListBox to change the style of all the items contained in the ListBox. To change the visual structure and visual behavior of a ListBoxItem, copy and modify its default style and template. For more information, see Control Customization.

If a dependency property for a ListBoxItem is set by its default style, the property might change from its default value when the ListBoxItem appears in the application. For more information, see Dependency Property Value Precedence. You can get the default style and template for ListBoxItem from ListBox Styles and Templates.

NoteNote:

Setting a visual property will only have an effect if that property is both present in ListBoxItem control's default template and is set by using a TemplateBinding. You can find a list of visual properties in the "Changing the Visual Structure of a Control" section of Customizing the Appearance of an Existing Control by Using a ControlTemplate.

Examples

The following example shows how to add ListBoxItem objects directly to a ListBox control using XAML.

XAML
<ListBox Width="150" Margin="0,5,0,5" SelectionChanged="PrintText" HorizontalAlignment="Left">
    <ListBoxItem Content="Item 1" />
    <ListBoxItem Content="Item 2" />
    <ListBoxItem Content="Item 3" />
    <ListBoxItem Content="Item 4" />
    <ListBoxItem Content="Item 5" />
</ListBox>
<TextBlock Name="textBlock1" />
Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.Control
          System.Windows.Controls..::.ContentControl
            System.Windows.Controls..::.ListBoxItem
              System.Windows.Controls..::.ComboBoxItem
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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags :


Page view tracker