Represents a selection control that combines a non-editable text box and a drop-down containing a list box that allows users to select an item from a list.
Namespace:
System.Windows.Controls
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "InvalidFocused", GroupName := "ValidationStates")> _
<TemplatePartAttribute(Name := "ContentPresenter", Type := GetType(ContentPresenter))> _
<TemplatePartAttribute(Name := "Popup", Type := GetType(Popup))> _
<TemplatePartAttribute(Name := "ContentPresenterBorder", Type := GetType(FrameworkElement))> _
<TemplatePartAttribute(Name := "DropDownToggle", Type := GetType(ToggleButton))> _
<TemplatePartAttribute(Name := "ScrollViewer", Type := GetType(ScrollViewer))> _
<TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "FocusedDropDown", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Valid", GroupName := "ValidationStates")> _
<TemplateVisualStateAttribute(Name := "InvalidUnfocused", GroupName := "ValidationStates")> _
Public Class ComboBox _
Inherits Selector
[TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "InvalidFocused", GroupName = "ValidationStates")]
[TemplatePartAttribute(Name = "ContentPresenter", Type = typeof(ContentPresenter))]
[TemplatePartAttribute(Name = "Popup", Type = typeof(Popup))]
[TemplatePartAttribute(Name = "ContentPresenterBorder", Type = typeof(FrameworkElement))]
[TemplatePartAttribute(Name = "DropDownToggle", Type = typeof(ToggleButton))]
[TemplatePartAttribute(Name = "ScrollViewer", Type = typeof(ScrollViewer))]
[TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "FocusedDropDown", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Valid", GroupName = "ValidationStates")]
[TemplateVisualStateAttribute(Name = "InvalidUnfocused", GroupName = "ValidationStates")]
public class ComboBox : Selector
XAML Object Element Usage
The text box either displays the current selection or is empty if there is no selected item. The following illustration shows a combo box control.
Combo Box control
.png)
Note: |
|---|
If you populate a combo box with items that inherit from UIElement, the selected item can only appear in the visual tree one time. This means if an item is selected, it will appear in the selection box, and not in the drop-down list of items. When it is no longer the selected item, it will reappear in the drop-down list. If items do not inherit from UIElement, they can appear in both the selection box and the drop-down list at the same time. |
You can use the ItemContainerGenerator to retrieve ComboBoxItem container objects using an index or the item contained in the ComboBoxItem. For example, if you have a data-bound ComboBox, and you want to get a ComboBoxItem based on its index, you can use the ContainerFromIndex method. If you want to retrieve the container based on the item, use the ContainerFromItem method.
Note: |
|---|
If you populate a combo box with items that inherit from UIElement, the selected item can only appear in the visual tree one time. This means if an item is selected, it will appear in the selection box, and not in the drop-down list of items. When it is no longer the selected item, it will reappear in the drop-down list. If items do not inherit from UIElement they can appear in both the selection box and the drop-down list at the same time. |
You can determine whether the ComboBoxItem is a selected item using IsSelected property.
Customizing the ComboBox Control
You can apply a style to each item displayed in the ComboBox by setting the ItemContainerStyle property.
To apply the same property settings to multiple ComboBox controls, use the Style property. To change the visual structure and visual behavior of a ComboBox, copy and modify its default style and template. For more information, see Control Customization.
If a dependency property for a ComboBox is set by its default style, the property might change from its default value when the ComboBox appears in the application. For more information, see Dependency Property Value Precedence. You can get the default style and template for ComboBox from ComboBox Styles and Templates.
The following example demonstrates binding a combo box to a collection of FontFamily objects.
Run this sample
Public Sub New()
MyBase.New()
InitializeComponent()
Dim fonts As New ObservableCollection(of FontFamily)
fonts.Add(New FontFamily("Arial"))
fonts.Add(New FontFamily("Courier New"))
fonts.Add(New FontFamily("Times New Roman"))
FontsCombo.DataContext = fonts
End Sub
public Page()
{
InitializeComponent();
ObservableCollection<FontFamily> fonts = new ObservableCollection<FontFamily>();
fonts.Add(new FontFamily("Arial"));
fonts.Add(new FontFamily("Courier New"));
fonts.Add(new FontFamily("Times New Roman"));
FontsCombo.DataContext = fonts;
}
<ComboBox x:Name="FontsCombo" Height="20" Width="150" ItemsSource="{Binding}" />
System..::.Object
System.Windows..::.DependencyObject
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Control
System.Windows.Controls..::.ItemsControl
System.Windows.Controls.Primitives..::.Selector
System.Windows.Controls..::.ComboBox
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