
ComboBox Class
Updated: September 2010
Represents a selection control with a drop-down list that can be shown or hidden by clicking the arrow on the control.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[TemplatePartAttribute(Name = "PART_Popup", Type = typeof(Popup))] [StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(ComboBoxItem))] [LocalizabilityAttribute(LocalizationCategory.ComboBox)] [TemplatePartAttribute(Name = "PART_EditableTextBox", Type = typeof(TextBox))] public class ComboBox : Selector
<ComboBox> Items </ComboBox>
The ComboBox allows the user to select an item from a drop-down list or optionally to enter new text in the text box of the control.
The IsEditable and IsReadOnly properties specify how the ComboBox behaves when the user does one of the following:
Enters a string to select an item in the ComboBox.
Enters a string that does not correspond to an item in the ComboBox.
Selects part of the string that is in the text box.
Copies or pastes a value into the text box.
The following table describes the actions the user can and cannot take, depending on the values of IsEditable and IsReadOnly.
IsReadOnly is true | IsReadOnly is false | |
---|---|---|
IsEditable is true |
|
|
IsEditable is false |
|
|
The IsReadOnly property has an effect only if IsEditable is true.
The ComboBox is an ItemsControl, so it can display content other than simple strings. For example, you can create a ComboBox that contains a list of images. When you have content other than strings in the ComboBox, a nonsensical string might appear in the ComboBox when the drop-down list is hidden. To display a string in the ComboBox when it contains non-string items, use the TextSearch.Text or TextSearch.TextPath attached property.
Content Model: ComboBox is a ItemsControl. Its content properties are Items and ItemsSource. For more information on the content model for ComboBox, see Controls Content Model Overview.
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.
The following example creates a ComboBox. The example populates the ComboBox by binding the ItemsSource property to a collection object of type VacationSpots. The example also creates a TextBlock that displays the selected item of the ComboBox.
<StackPanel Grid.Column="0" Grid.Row="6"> <StackPanel.Resources> <src:VacationSpots x:Key="myVacations"/> </StackPanel.Resources> <ComboBox Name="comboBox1" ItemsSource="{StaticResource myVacations}" Text="My Cities" IsEditable="true" IsReadOnly="true"/> <TextBlock Text="{Binding ElementName=comboBox1, Path=SelectedItem}"/> </StackPanel>
The following example defines the collection object that the ComboBox in the preceding example binds to.
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.ComboBox
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.
Date | History | Reason |
---|---|---|
September 2010 | Added example. | Information enhancement. |
February 2009 | Described how default styles change dependency properties. | Customer feedback. |
July 2008 | Added new members: SelectionBoxItemStringFormat property, SelectionBoxItemStringFormatProperty field. | SP1 feature change. |