Class: ComboBox

Namespace: System.Windows.Controls

Library: PresentationFramework

A ComboBox is similar to a ListBox but the user needs to "open" the list to see the items.

For the creation and use of a ComboBox you will need to use another Class: ComboBoxItem

Better to play a real demo looking the real code.

TIP: We don't use Height to a ComboBox. It will have the height of one line.

The Window1.xaml file is:

<Window  
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    x:Class="DMU.Window1"
    Title="DMU Tutorials Serie"
    Top="100"
    Left= "100"
    Width="400"
    Height="400"
>

 <Canvas  Width="400"  Height="400" Background="DeepSkyBlue"  >
  <TextBlock  Name="Text1"   Canvas.Top="50" Canvas.Left="100"   FontSize="14">Press buttons to populate list</TextBlock>
  <Button   Name="Bt1" Canvas.Top="100" Canvas.Left="100" Click = "Hat">Hat</Button>
  <Button  Name="Bt2" Canvas.Top="100" Canvas.Left="200" Click = "Shoe">Shoe</Button>
  <Button   Name="Bt3" Canvas.Top="100" Canvas.Left="300" Click = "Shirt">Shirt</Button>
   <ComboBox Name="TheCB" Canvas.Top="150" Canvas.Left="100" Width="100"   SelectionMode="Single" SelectionChanged="Print" >
    <ComboBoxItem>Trousers</ComboBoxItem>
   </ComboBox>
   <TextBlock    Canvas.Top="250" Canvas.Left="100"   FontSize="14"  >Click one to select</TextBlock>
   <TextBox Name = "TheItem" Canvas.Top="300" Canvas.Left="100" FontSize="14"/>
 </Canvas>
</Window> 

And the Window1.xaml.cs file is:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
 
namespace DMU{
	 
public partial class Window1 : Window{
  
  public Window1(){
   InitializeComponent();
  }
  private void Hat(object sender, RoutedEventArgs e){
     ComboBoxItem li  = new ComboBoxItem();
     li.Content =  "Hat" ;
     TheCB.Items.Add(li);
     Bt1.IsEnabled = false;
   }
  private void Shoe(object sender, RoutedEventArgs e){
     ComboBoxItem li  = new ComboBoxItem();
     li.Content = "Shoe" ;
     TheCB.Items.Add(li);
     Bt2.IsEnabled = false;
  }
  private void Shirt(object sender, RoutedEventArgs e){
    ComboBoxItem li = new ComboBoxItem();
    li.Content = "Shirt";
    TheCB.Items.Add(li);
    Bt3.IsEnabled = false;
  }
  void Print(object sender, SelectionChangedEventArgs args){
   ComboBoxItem lboxitem = new ComboBoxItem();
   lboxitem = TheCB.SelectedItem as ComboBoxItem;
   TheItem.Text = "Selected: " + lboxitem.Content.ToString();

  }   
 } 
} 

IMPORTANT: We are not presenting the other 3 usual files of the application.

Pay attention how we are doing a conversion (as ComboBoxItem). This works for some types.

You can run the application. Please, you need to liberate the firewall.

After this introduction, some properties:

Background

The value will be a color.

BorderBrush

Also a color.

BorderThickness

FontFamily

FontSize

FontStyle

FontWeight

Foreground

The color of the text.

Height

Items

Very important. Is the collection of data. You need to use the method Add to include a new ComboBoxItem. Something like in the sample:

  ...
  ComboBoxItem li  = new ComboBoxItem();
  li.Content = "Shoe" ;
  TheCB.Items.Add(li);
  ...

Margin

Name

Opacity

There are some properties for the capture of the selection:


 
SelectedIndex:   Gets or sets the index of the   item in the current selection or returns 
                    negative one (-1) if the selection is empty.  
SelectedItem:    Gets or sets the   item in the current selection or returns null if the selection is empty.  
 

Visibility

Width

Some events. To be overrided.

The more important (you can see how to use it at the sample):

SelectionChanged

KeyDown

KeyUp

And the usual mouse events:


  MouseDoubleClick    
  MouseDown    
  MouseEnter    
  MouseLeave    
  MouseLeftButtonDown    
  MouseLeftButtonUp    
  MouseMove    
  MouseRightButtonDown   
  MouseRightButtonUp    
  MouseUp    
   



PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE