System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
ComboBox Class

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)
Syntax

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
Visual Basic (Usage)
Dim instance As ComboBox
C#
[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
<ComboBox .../>
Remarks

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

Silverlight ComboBox Control
NoteNote:

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.

ComboBox is an ItemsControl. Its content properties are Items and ItemsSource. For more information, see Control Content Models.

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.

NoteNote:

Setting a visual property will only have an effect if that property is both present in ComboBox 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 in Customizing the Appearance of an Existing Control by Using a ControlTemplate.

Examples

The following example demonstrates binding a combo box to a collection of FontFamily objects.

Run this sample

Visual Basic
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
C#
 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;
}
XAML
<ComboBox x:Name="FontsCombo" Height="20" Width="150" ItemsSource="{Binding}" />
Inheritance Hierarchy

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