Export (0) Print
Expand All

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.

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

public class ComboBox extends 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

  • Cannot select an item in the ComboBox by entering a string.

  • Cannot enter a string that does not correspond to an item in the ComboBox.

  • Can select part of the string in the ComboBox text box.

  • Can copy the string in the ComboBox text box, but cannot paste a string into the ComboBox text box.

  • Can select an item in the ComboBox by entering a string.

  • Can enter a string that does not correspond to an item in the ComboBox.

  • Can select part of the string in the ComboBox text box.

  • Can copy or paste the string in the ComboBox text box.

IsEditable is false

  • Can select an item in the ComboBox by entering a string.

  • Cannot enter a string that does not correspond to an item in the ComboBox.

  • Cannot select part of the string in the ComboBox.

  • Cannot copy or paste the string in the ComboBox.

  • Can select an item in the ComboBox by entering a string.

  • Cannot enter a string that does not correspond to an item in the ComboBox.

  • Cannot select part of the string in the ComboBox.

  • Cannot copy or paste the string in the ComboBox.

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.

No code example is currently available or this language may not be supported.

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.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.

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.

.NET Framework

Supported in: 3.5, 3.0

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.

Community Additions

ADD
Show:
© 2014 Microsoft