Display a Message When the ListView Becomes Empty in Windows Store Apps

In my experience of creating Windows Store apps I have noticed that there are times where, once a list view no longer has any items in it, it is beneficial to let the user know that the list view they were looking at no longer has any items.
 
This is very simple to do and can be done with very little coding. To get things started we need to create a blank Windows Store app project as in the following:


Now that we have our project created, we are greeted with a blank XAML page. Let's provide some controls for us to use, such as a ListView, a TextBlock and a Button.
  1. <Page  
  2.     x:Class="CSharpCornerTestProject.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:CSharpCornerTestProject"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.       
  10.     <Page.Resources>  
  11.         <DataTemplate x:Key="listViewItems">  
  12.             <StackPanel>  
  13.                 <TextBlock  
  14.                     Margin="5,5,5,5"  
  15.                     Text="{Binding Name}"  
  16.                     Style="{StaticResource BaseTextBlockStyle}"/>  
  17.                   
  18.                 <TextBlock  
  19.                     Margin="5,5,5,5"  
  20.                     Text="{Binding Ripeness}"  
  21.                     Style="{StaticResource BaseTextBlockStyle}"/>  
  22.             </StackPanel>  
  23.         </DataTemplate>  
  24.     </Page.Resources>  
  25.   
  26.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  27.         <ListView  
  28.             x:Name="listViewTest"  
  29.             Margin="5,5,5,5"  
  30.             VerticalAlignment="Center"  
  31.             HorizontalAlignment="Center"  
  32.             ItemsSource="{Binding}"  
  33.             SelectionMode="None"  
  34.             IsItemClickEnabled="False"  
  35.             ItemTemplate="{StaticResource listViewItems}"  
  36.             ContainerContentChanging="listViewUpdated">  
  37.         </ListView>  
  38.           
  39.         <TextBlock  
  40.             x:Name="listViewNoItems"  
  41.             Margin="5,5,5,5"  
  42.             VerticalAlignment="Center"  
  43.             HorizontalAlignment="Center"  
  44.             Text="There are no fruits in your list to display!"  
  45.             Style="{StaticResource BaseTextBlockStyle}"  
  46.             Visibility="Collapsed"/>  
  47.           
  48.         <Button  
  49.             Width="150"  
  50.             Height="50"  
  51.             Margin="20"  
  52.             VerticalAlignment="Center"  
  53.             HorizontalAlignment="Right"  
  54.             Content="Clear fruit"  
  55.             Click="clearFruitBasket"/>  
  56.     </Grid>  
  57. </Page>
Notice the "ContainerContentChanging" event on the list view? That is what we will be using to detect if the list view has any items left at all.
 
Now, delving into the code side of things in our MainPage.xaml.cs file:
  1. using System.Collections.ObjectModel;  
  2. using Windows.UI.Xaml;  
  3. using Windows.UI.Xaml.Controls;  
  4.   
  5. namespace CSharpCornerTestProject  
  6. {  
  7.     public class Fruit  
  8.     {  
  9.         public string Name { getset; }  
  10.         public string Ripeness { getset; }  
  11.     }  
  12.   
  13.     public sealed partial class MainPage : Page  
  14.     {  
  15.         private ObservableCollection<Fruit> fruitList = new ObservableCollection<Fruit>();  
  16.         public MainPage()  
  17.         {  
  18.             this.InitializeComponent();  
  19.   
  20.             fruitList.Add(new Fruit() { Name = "Apple", Ripeness = "Ok" });  
  21.             fruitList.Add(new Fruit() { Name = "Banana", Ripeness = "Bad" });  
  22.             fruitList.Add(new Fruit() { Name = "Kiwi", Ripeness = "Rotten" });  
  23.   
  24.             listViewTest.ItemsSource = fruitList;  
  25.         }  
  26.   
  27.         private void listViewUpdated(ListViewBase sender, ContainerContentChangingEventArgs args)  
  28.         {  
  29.             if (listViewTest.Items.Count == 0)  
  30.             {  
  31.                 listViewNoItems.Visibility = Visibility.Visible;  
  32.                 listViewTest.Visibility = Visibility.Collapsed;  
  33.             }  
  34.             else  
  35.             {  
  36.                 listViewNoItems.Visibility = Visibility.Collapsed;  
  37.                 listViewTest.Visibility = Visibility.Visible;  
  38.             }  
  39.         }  
  40.   
  41.         private void clearFruitBasket(object sender, RoutedEventArgs e)  
  42.         {  
  43.             // clear the fruit list!  
  44.             if (fruitList != null)  
  45.                 fruitList.Clear();  
  46.         }  
  47.     }  

You will seethat at the top we have made a very simple class for storing some information for our list view. When the app is loaded it will create a new instance of ObservableCollection<Fruit> for us to add some fruits to.
  1. private ObservableCollection<Fruit> fruitList = new ObservableCollection<Fruit>(); 
When the MainPage is loaded the constructor will be fired and this is where our fruit basket gets some items added to it. In the code example we add three items (Apple, Banana and Kiwi) into the fruitList ObservableCollection. 
  1. public MainPage()      
  2. {      
  3.     this.InitializeComponent();      
  4.     
  5.     fruitList.Add(new Fruit() { Name = "Apple", Ripeness = "Ok" });      
  6.     fruitList.Add(new Fruit() { Name = "Banana", Ripeness = "Bad" });      
  7.     fruitList.Add(new Fruit() { Name = "Kiwi", Ripeness = "Rotten" });     
  8. }     
Once we've done that, the bulk of setting things up is done with, just set the item source of our list view to the ObservableCollection that is storing our information on our fruit basket.
  1. public MainPage()    
  2. {    
  3.     this.InitializeComponent();    
  4.     
  5.     fruitList.Add(new Fruit() { Name = "Apple", Ripeness = "Ok" });    
  6.     fruitList.Add(new Fruit() { Name = "Banana", Ripeness = "Bad" });    
  7.     fruitList.Add(new Fruit() { Name = "Kiwi", Ripeness = "Rotten" });    
  8.     
  9.     listViewTest.ItemsSource = fruitList;

Excellent! Alright, so we've got our list full of fruits, but how will we know when the items have all vanished? That is where the ContainerContentChanging="listViewUpdated" part of the XAML we did earlier is relevant.
  1. private void listViewUpdated(ListViewBase sender, ContainerContentChangingEventArgs args)  
  2. {  
  3.     if (listViewTest.Items.Count == 0)  
  4.     {  
  5.         listViewNoItems.Visibility = Visibility.Visible;  
  6.         listViewTest.Visibility = Visibility.Collapsed;  
  7.     }  
  8.     else  
  9.     {  
  10.         listViewNoItems.Visibility = Visibility.Collapsed;  
  11.         listViewTest.Visibility = Visibility.Visible;  
  12.     }  

This event will be called whenever there is a change to the items inside the list view that we created earlier. It simply checks the item count of our ListView control and if it is empty it hides itself and brings the TextBlock telling us that there are no items in the view.

We can clear the list view by simply clicking on our Button control that will empty the ObservableCollection if it has items in it.
  1. private void clearFruitBasket(object sender, RoutedEventArgs e)  
  2. {  
  3.     // clear the fruit list!  
  4.     if (fruitList != null)  
  5.         fruitList.Clear();  

Once the event fires it will clear our basket and you should now have a message on the screen telling us that the list view doesn't have any items in it anymore.

.

And that is all there is to it; a very simple method of showing a message once your list view is empty if the user needs some information about why their basket of fruit has suddenly vanished from the screen!
 
Thanks for reading! If you want to keep up-to-date with what I'm working on then be sure you follow me on Twitter, @Forceh91.