XAML ListBox

The XAML <ListBox> element represents a WPF ListBox control. 
  1. <ListBox></ListBox>    
Similar to any other XAML element, the Width and Height properties of ListBox represent the width and the height of a ListBox. The Name property represents the name of the control that is a unique identifier of a control. The Margin property tells the location of a ListBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.
The following code example is used to set the name, height and width of a ListBox control. The code also sets the horizontal alignment to left and the vertical alignment to top. 
  1. <ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"    
  2. VerticalAlignment="Top" Width="194" Height="200" />    
The <ListBoxItem> element represents an item of a ListBox. A ListBox is a collection of ListBoxItem elements. The following code example adds several ListBoxItem elements to a ListBox. 
  1. <ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"     
  2.          VerticalAlignment="Top" Width="194" Height="200">    
  3.     <ListBoxItem Content="Coffie"></ListBoxItem>    
  4.     <ListBoxItem Content="Tea"></ListBoxItem>    
  5.     <ListBoxItem Content="Orange Juice"></ListBoxItem>    
  6.     <ListBoxItem Content="Milk"></ListBoxItem>    
  7.     <ListBoxItem Content="Iced Tea"></ListBoxItem>    
  8.     <ListBoxItem Content="Mango Shake"></ListBoxItem>    
  9. </ListBox>    
Each ListBoxItem is its own independent element and can have its own properties.
The Foreground and Background attributes of ListBoxItem represents the background and foreground colors of the item. The following code snippet sets the background and foreground colors of a ListBoxItem. 
  1. <ListBoxItem Background="LightCoral" Foreground="Red" Content="Coffie"></ListBoxItem>    
The FontFamily, FontSize and FontWeight are used to set a font of a ListBoxItem. The following code snippet sets the font to verdana, the size to 12 and bold of a ListBoxItem. I set the following properties of ListBoxItems. 
  1. <ListBoxItem Background="LightCoral" Foreground="Red" Content="Coffie"    
  2.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListBoxItem>    
  3. I set the following properties of ListBoxItems.  
  5. <ListBoxItem Background="LightCoral" Foreground="Red" Content="Coffie"    
  6.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListBoxItem>    
  7.             <ListBoxItem Background="LightGray" Foreground="Black" Content="Tea"    
  8.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListBoxItem>    
  9.             <ListBoxItem Background="LightBlue" Foreground="Purple" Content="Orange Juice"    
  10.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListBoxItem>    
  11.             <ListBoxItem Background="LightGreen" Foreground="Green" Content="Milk"    
  12.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListBoxItem>    
  13.             <ListBoxItem Background="LightBlue" Foreground="Blue" Content="Iced Tea"    
  14.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListBoxItem>    
  15.             <ListBoxItem Background="LightSlateGray" Foreground="Orange" Content="Mango Shake"    
  16.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListBoxItem>   
In this article, I explained how to create and use a ListBox control available in WPF. We saw how to add items to a ListBox, change item properties and add images add check boxes. In the end of this article, we saw how data binding works in ListBox and how to bind a ListBox with data coming from objects, databases and other controls.

To avoid duplicate content, the detailed tutorial is moved here: WPF ListBox Tutorial