WPF - Combobox



A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item. The hierarchical inheritance of ComboBox class is as follows −

Hierarchical of Combobox

Commonly Used Properties of ComboBox

Sr.No. Property & Description
1

Background

Gets or sets a brush that provides the background of the control. (Inherited from Control)

2

BorderThickness

Gets or sets the border thickness of a control. (Inherited from Control)

3

FontFamily

Gets or sets the font used to display text in the control. (Inherited from Control)

4

FontSize

Gets or sets the size of the text in this control. (Inherited from Control)

5

FontStyle

Gets or sets the style in which the text is rendered. (Inherited from Control)

6

FontWeight

Gets or sets the thickness of the specified font. (Inherited from Control)

7

Foreground

Gets or sets a brush that describes the foreground color. (Inherited from Control)

8

GroupStyle

Gets a collection of GroupStyle objects that define the appearance of each level of groups. (Inherited from ItemsControl)

9

Header

Gets or sets the content for the control's header.

10

Height

Gets or sets the suggested height of a FrameworkElement. (Inherited from FrameworkElement)

11

HorizontalAlignment

Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (Inherited from FrameworkElement)

12

IsDropDownOpen

Gets or sets a value that indicates whether the drop-down portion of the ComboBox is currently open.

13

IsEditable

Gets a value that indicates whether the user can edit text in the text box portion of the ComboBox. This property always returns false.

14

IsEnabled

Gets or sets a value indicating whether the user can interact with the control. (Inherited from Control)

15

Margin

Gets or sets the outer margin of a FrameworkElement. (Inherited from FrameworkElement)

16

Name

Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML-declared object by this name. (Inherited from FrameworkElement)

17

Opacity

Gets or sets the degree of the object's opacity. (Inherited from UIElement)

18

SelectedIndex

Gets or sets the index of the selected item. (Inherited from Selector)

19

SelectedItem

Gets or sets the selected item. (Inherited from Selector)

20

SelectedValue

Gets or sets the value of the selected item, obtained by using the SelectedValuePath. (Inherited from Selector)

21

Style

Gets or sets an instance Style that is applied for this object during layout and rendering. (Inherited from FrameworkElement)

22

VerticalAlignment

Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (Inherited from FrameworkElement)

23

Width

Gets or sets the width of a FrameworkElement. (Inherited from FrameworkElement)

24

ItemsSource

Gets or sets an object source used to generate the content of the ItemsControl. (Inherited from ItemsControl)

Commonly Used Methods of ComboBox

Sr.No. Method & Description
1

Arrange

Positions child objects and determines a size for a UIElement. Parent objects that implement custom layout for their child elements should call this method from their layout override implementations to form a recursive layout update. (Inherited from UIElement)

2

FindName

Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)

3

Focus

Attempts to set the focus on the control. (Inherited from Control)

4

GetValue

Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)

5

IndexFromContainer

Returns the index to the item that has the specified, generated container. (Inherited from ItemsControl)

6

OnDragEnter

Called before the DragEnter event occurs. (Inherited from Control)

7

OnDragLeave

Called before the DragLeave event occurs. (Inherited from Control)

8

OnDragOver

Called before the DragOver event occurs. (Inherited from Control)

9

OnDrop

Called before the Drop event occurs. (Inherited from Control)

10

OnKeyDown

Called before the KeyDown event occurs. (Inherited from Control)

11

OnKeyUp

Called before the KeyUp event occurs. (Inherited from Control)

12

OnLostFocus

Called before the LostFocus event occurs. (Inherited from Control)

13

ReadLocalValue

Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)

14

SetBinding

Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)

15

SetValue

Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)

Commonly Used Events of ComboBox

Sr.No. Event & Description
1

DragEnter

Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)

2

DragLeave

Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)

3

DragOver

Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)

4

DragStarting

Occurs when a drag operation is initiated. (Inherited from UIElement)

5

Drop

Occurs when the input system reports an underlying drop event with this element as the drop target. (Inherited from UIElement)

6

DropCompleted

Occurs when a drag-and-drop operation is ended. (Inherited from UIElement)

7

DropDownClosed

Occurs when the drop-down portion of the ComboBox closes.

8

DropDownOpened

Occurs when the drop-down portion of the ComboBox opens.

9

GotFocus

Occurs when a UIElement receives focus. (Inherited from UIElement)

10

IsEnabledChanged

Occurs when the IsEnabled property changes. (Inherited from Control)

11

KeyDown

Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)

12

KeyUp

Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)

13

LostFocus

Occurs when a UIElement loses focus. (Inherited from UIElement)

14

SelectionChanged

Occurs when the currently selected item changes. (Inherited from Selector)

15

SizeChanged

Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (Inherited from FrameworkElement)

Example

  • Let’s create a new WPF project with the name WPFComboBoxControl.

  • Drag two comboboxes and two textboxes from a toolbox and set the following properties in the properties window.

Controls Property Value
Combobox1 isEditable False
Name comboBox
Width 90
Combobox2 isEditable True
Name comboBox1
Width 90
Textbox1 Name textBox
Width 300
Textbox2 Name textBox1
Width 300
  • Now switch to XAML window in which you will see the XAML tags for comboboxes and textboxes.

  • Add some more properties combobox items and selection event, as shown in the following XAML code.

<Window x:Class = "WPFComboBoxControl.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFComboBoxControl" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid>
	
      <ComboBox x:Name = "comboBox" HorizontalAlignment = "Left" 
         Margin = "80,53,0,0" VerticalAlignment = "Top" Width = "120"
         SelectionChanged = "Combo_SelectionChanged"> 
			
         <ComboBoxItem Content = "Item #1" /> 
         <ComboBoxItem Content = "Item #2" /> 
         <ComboBoxItem Content = "Item #3" />
      </ComboBox>
		
      <ComboBox x:Name = "comboBox1" HorizontalAlignment = "Left"
         Margin = "80,153,0,0" VerticalAlignment = "Top" Width = "120"
         IsEditable = "True"
         SelectionChanged = "Combo1_SelectionChanged">
			
         <ComboBoxItem Content = "Item #1" /> 
         <ComboBoxItem Content = "Item #2" /> 
         <ComboBoxItem Content = "Item #3" />
      </ComboBox>
		
      <TextBox x:Name = "textBox" HorizontalAlignment = "Left" 
         Height = "23" Margin = "253,53,0,0" TextWrapping = "Wrap"
         VerticalAlignment = "Top" Width = "200" /> 
			
      <TextBox x:Name = "textBox1" HorizontalAlignment = "Left"
         Height = "23" Margin = "253,152,0,0" TextWrapping = "Wrap"
         VerticalAlignment = "Top" Width = "200" />
			
   </Grid> 
	
</Window>

Here is the C# code in which the selection changed events are implemented.

using System.Windows; 
using System.Windows.Controls;

namespace WPFComboBoxControl { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary> 
	
   public partial class MainWindow : Window { 
	
      public MainWindow() { 
         InitializeComponent(); 
      }
		
      private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e) { 
         textBox.Text = comboBox.SelectedItem.ToString(); 
      }
		
      private void Combo1_SelectionChanged(object sender, SelectionChangedEventArgs e) { 
         textBox1.Text = comboBox1.SelectedItem.ToString(); 
      } 
		
   }
}

When you compile and execute the above code, it will produce the following window. When you select an item, it will be displayed on the textbox.

select an item

We recommend that you execute the above example code and try some other properties and events of the combobox control.

wpf_controls.htm
Advertisements