Represents a selectable item in a ListBox.
Namespace:
System.Windows.Controls
Assembly:
System.Windows (in System.Windows.dll)
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
Dim instance As ListBoxItem
[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.
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.
The following example shows how to add ListBoxItem objects directly to a ListBox control using 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" />
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
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources