Using XAML ListView in WPF

A XAML ListView element represents a ListView control in WPF. This tutorial shows you how to create and use a ListView control available in WPF.

Introduction

The ListView tag represents a WPF ListView control in XAML.

  1. <ListView></ListView>  
The Width and Height properties represent the width and the height of a ListView. The Name property represents the name of the control that is a unique identifier of a control. The Margin property specifies the location of a ListView on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.

The following code snippet sets the name, height and width of a ListView control. The code also sets the horizontal alignment to left and the vertical alignment to top.
  1. <ListView Margin="10,10,0,13" Name="ListView1" HorizontalAlignment="Left"   
  2.                  VerticalAlignment="Top" Width="194" Height="200" />  
Adding ListView Items

A ListView control hosts a collection of ListViewItem items. The following code snippet adds items to a ListView control.
  1. <ListView Margin="10,10,0,13" Name="ListView1" HorizontalAlignment="Left"   
  2.          VerticalAlignment="Top" Width="194" Height="200">  
  3.     <ListViewItem Content="Coffie"></ListViewItem>  
  4.     <ListViewItem Content="Tea"></ListViewItem>  
  5.     <ListViewItem Content="Orange Juice"></ListViewItem>  
  6.     <ListViewItem Content="Milk"></ListViewItem>  
  7.     <ListViewItem Content="Iced Tea"></ListViewItem>  
  8.     <ListViewItem Content="Mango Shake"></ListViewItem>  
  9. </ListView>  
The preceding code generates Figure 1.

ListView with items
                                              Figure 1. ListView with items

Adding ListView Items Dynamically

In the previous section, we saw how to add items to a ListView at design-time from XAML. We can add items to a ListView from the code.

Now we change our UI and add a TextBox and a button control to the page. The XAML code for the TextBox and Button controls look like the following:
  1. <TextBox Height="23" HorizontalAlignment="Left" Margin="8,14,0,0"   
  2.                  Name="textBox1" VerticalAlignment="Top" Width="127" />  
  3. <Button Height="23" Margin="140,14,0,0" Name="button1" VerticalAlignment="Top"   
  4.                 HorizontalAlignment="Left" Width="76" Click="button1_Click">  
  5.             Add Item  
  6. </Button>  
The final UI looks as in Figure 2.

add item
                                                             Figure 2.

On the button click event handler, we add the contents of the TextBox to the ListView by calling the ListView.Items.Add method. The following code adds TextBox contents to the ListView items.
  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. ListView1.Items.Add(textBox1.Text);  
  4. }  
On button click event handler, we add the content of the TextBox to the ListView by calling the ListView.Items.Add method.

Now if you enter text into the TextBox and click the Add Item button, it will add the contents of the TextBox to the ListView.

*Adding ListView items dynamically
                                Figure 3. Adding ListView items dynamically

Deleting ListView Items

We can use a ListView.Items.Remove or ListView.Items.RemoveAt method to delete an item from the collection of items in the ListView. The RemoveAt method takes the index of the item in the collection.

Now, we modify our application and add a new button called Delete Item. The XAML code for this button looks as in the following.
  1. <Button Height="23" Margin="226,14,124,0" Name="DeleteButton"   
  2.         VerticalAlignment="Top" Click="DeleteButton_Click">  
  3.     Delete Item</Button>  
The button click event handler looks as in the following. On this button click, we find the index of the selected item and call the ListView.Items.RemoveAt method as in the following.
  1. private void DeleteButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    ListView1.Items.RemoveAt  
  4.        (ListView1.Items.IndexOf(ListView1.SelectedItem));                    
  5. }  
Formatting and Styling

Formatting ListView Items

The Foreground and Background attributes of a ListViewItem represents the background and foreground colors of the item. The following code snippet sets the background and foreground colors of a ListViewItem.
  1. <ListViewItem Background="LightCoral" Foreground="Red" Content="Coffie"></ListViewItem>  
The FontFamily, FontSize and FontWeight are used to set a font of a ListViewItem. The following code snippet sets font verdana, size 12 and bold of a ListViewItem.
  1. <ListViewItem Background="LightCoral" Foreground="Red" Content="Coffie"  
  2.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListViewItem>  
I set the following properties of ListViewItems.
  1. <ListViewItem Background="LightCoral" Foreground="Red" Content="Coffie"  
  2.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListViewItem>  
  3.             <ListViewItem Background="LightGray" Foreground="Black" Content="Tea"  
  4.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListViewItem>  
  5.             <ListViewItem Background="LightBlue" Foreground="Purple" Content="Orange Juice"  
  6.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListViewItem>  
  7.             <ListViewItem Background="LightGreen" Foreground="Green" Content="Milk"  
  8.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListViewItem>  
  9.             <ListViewItem Background="LightBlue" Foreground="Blue" Content="Iced Tea"  
  10.                          FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ListViewItem>  
  11.             <ListViewItem Background="LightSlateGray" Foreground="Orange" Content="Mango Shake"  
  12.                          FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ListViewItem>  
The new ListView looks as in Figure 4.

Formatted ListView
                                             Figure 4. Formatted ListView

Displaying Images in a ListView

We can put any controls inside a ListViewItem such as an image and text. To display an image beside some text, I simply put an Image and TextBlock control within a StackPanel. The Image.Source property takes the name of the image you would like to display in the Image control and the TextBlock.Text property takes a string that you would like to display in the TextBlock.

The following code snippet adds an image and text to a ListViewItem.
  1. <ListViewItem Background="LightCoral" Foreground="Red"   
  2.              FontFamily="Verdana" FontSize="12" FontWeight="Bold">  
  3.     <StackPanel Orientation="Horizontal">  
  4.         <Image Source="coffie.jpg" Height="30"></Image>  
  5.         <TextBlock Text="Coffie"></TextBlock>  
  6.     </StackPanel>  
  7. </ListViewItem>  
After changing my code for all 5 ListViewItems, the ListView looks as in Figure 5.

ListViewItems with Image and text
                           Figure 5. ListViewItems with Image and text

ListView with CheckBoxes

If you put a CheckBox control inside ListViewItems, you generate a ListView control with checkboxes in it. The CheckBox can host controls within it as well. For instance, we can put an image and text block as the contents of a CheckBox.

The following code snippet adds a CheckBox with an image and text to a ListViewItem.
  1. <ListViewItem Background="LightCoral" Foreground="Red"   
  2.              FontFamily="Verdana" FontSize="12" FontWeight="Bold">                  
  3.         <CheckBox Name="CoffieCheckBox">  
  4.             <StackPanel Orientation="Horizontal">  
  5.             <Image Source="coffie.jpg" Height="30"></Image>  
  6.             <TextBlock Text="Coffie"></TextBlock>  
  7.         </StackPanel>  
  8.     </CheckBox>  
  9. </ListViewItem>  
I change the code of ListViewItems and add the following CheckBoxes to the items. As you may see, I have set the name of the CheckBoxes using the Name property. If you need to access these CheckBoxes, you may access them in the code using their Name property.
  1.  <ListViewItem Background="LightCoral" Foreground="Red"   
  2.              FontFamily="Verdana" FontSize="12" FontWeight="Bold">                  
  3.         <CheckBox Name="CoffieCheckBox">  
  4.             <StackPanel Orientation="Horizontal">  
  5.             <Image Source="coffie.jpg" Height="30"></Image>  
  6.             <TextBlock Text="Coffie"></TextBlock>  
  7.         </StackPanel>  
  8.     </CheckBox>  
  9. </ListViewItem>  
  10. <ListViewItem Background="LightGray" Foreground="Black"   
  11.              FontFamily="Georgia" FontSize="14" FontWeight="Bold">  
  12.     <CheckBox Name="TeaCheckBox">  
  13.         <StackPanel Orientation="Horizontal">  
  14.             <Image Source="tea.jpg" Height="30"></Image>  
  15.             <TextBlock Text="Tea"></TextBlock>  
  16.         </StackPanel>  
  17.     </CheckBox>  
  18. </ListViewItem>  
  19. <ListViewItem Background="LightBlue" Foreground="Purple"   
  20.              FontFamily="Verdana" FontSize="12" FontWeight="Bold">  
  21.     <CheckBox Name="OrangeJuiceCheckBox">  
  22.         <StackPanel Orientation="Horizontal">  
  23.             <Image Source="OrangeJuice.jpg" Height="40"></Image>  
  24.             <TextBlock Text="OrangeJuice"></TextBlock>  
  25.         </StackPanel>  
  26.     </CheckBox>  
  27. </ListViewItem>  
  28. <ListViewItem Background="LightGreen" Foreground="Green"   
  29.              FontFamily="Georgia" FontSize="14" FontWeight="Bold">  
  30.     <CheckBox Name="MilkCheckBox">  
  31.         <StackPanel Orientation="Horizontal">  
  32.             <Image Source="Milk.jpg" Height="30"></Image>  
  33.             <TextBlock Text="Milk"></TextBlock>  
  34.         </StackPanel>  
  35.     </CheckBox>  
  36. </ListViewItem>  
  37. <ListViewItem Background="LightBlue" Foreground="Blue"   
  38.              FontFamily="Verdana" FontSize="12" FontWeight="Bold">  
  39.     <CheckBox Name="IcedTeaCheckBox">  
  40.         <StackPanel Orientation="Horizontal">  
  41.             <Image Source="IcedTea.jpg" Height="30"></Image>  
  42.             <TextBlock Text="Iced Tea"></TextBlock>  
  43.         </StackPanel>  
  44.     </CheckBox>  
  45. </ListViewItem>  
  46. <ListViewItem Background="LightSlateGray" Foreground="Orange"  
  47.              FontFamily="Georgia" FontSize="14" FontWeight="Bold">  
  48.     <CheckBox Name="MangoShakeCheckBox">  
  49.         <StackPanel Orientation="Horizontal">  
  50.             <Image Source="MangoShake.jpg" Height="30"></Image>  
  51.             <TextBlock Text="Mango Shake"></TextBlock>  
  52.         </StackPanel>  
  53.     </CheckBox>  
  54. </ListViewItem>  
Now, the new ListView looks as in Figure 6.

ListView with CheckBoxes
                                    Figure 6. ListView with CheckBoxes

Data Binding in ListView

The ItemsSource property of ListView binds a collection of IEnuemerable such as an ArrayList to the ListView control.

The following code snippet creates an ArrayList object.
  1. /// <summary>  
  2. /// Generate data. This method can bring data from a database or XML file  
  3. /// or from a Web service or generate data dynamically  
  4. /// </summary>  
  5. /// <returns></returns>  
  6. private ArrayList LoadListViewData()  
  7. {  
  8.     ArrayList itemsList = new ArrayList();  
  9.     itemsList.Add("Coffie");  
  10.     itemsList.Add("Tea");  
  11.     itemsList.Add("Orange Juice");  
  12.     itemsList.Add("Milk");  
  13.     itemsList.Add("Mango Shake");  
  14.     itemsList.Add("Iced Tea");  
  15.     itemsList.Add("Soda");  
  16.     itemsList.Add("Water");  
  17.     return itemsList;  
  18. }  
On the Window loaded event, we create and load data items into the ListView by setting the ItemsSource property to an ArrayList.
  1. private void Window_Loaded(object sender, RoutedEventArgs e)  
  2. {  
  3.     // Get data from somewhere and fill in my local ArrayList  
  4.     myDataList = LoadListViewData();  
  5.     // Bind ArrayList with the ListView  
  6.     LeftListView.ItemsSource = myDataList;              
  7. }  
The collection items are being displayed as in Figure 7.

ListView bound to a collection object
    Figure 7. ListView bound to a collection object

Summary

In this article, I discussed how to create and use a ListView control available in WPF. We saw how to add items to a ListView, change item properties, add images add check boxes. In the end of this article, we saw how data binding works in a ListView.