Universal Windows Platform ListBox

One of the common and important control of Windows 10 Universal App is List Box control. It is really helpful for making a long list of elements. You can populate it with some data, pictures or anything that you want to show in your application. List Box can be implemented easily only by XAML. In this example, we are going to use our previous project SplitView control and make the Split View Pane items with the help of List Box. So, let’s get started.

Creating a Project with Split View Control

First of all, create a new project and in MainPage.xaml inside the Grid make a new SplitView control.

  1. <SplitView x:Name="SplitView" OpenPaneLength="200" CompactPaneLength="50"   
  2.         DisplayMode="CompactOverlay" IsPaneOpen="False" PaneBackground="DarkGray" >  
  3.     <SplitView.Pane>  
  4.         <!--Pane Items-->  
  5.     </SplitView.Pane>  
  6.     <SplitView.Content>  
  7.         <!--Content View-->  
  8.     </SplitView.Content>  
  9. </SplitView>  
                                                Listing 1

Previously, we have created the Pane items with Radio Buttons with custom styles. But, now we will work with ListBox items, and the work will be much easier for us.

Creating ListBox Items

ListBox is a simple control with inline elements of ListBox items. The basic structure of ListBox is as in the following code snippet:
  1. <ListBox>  
  2. <ListBoxItem Content="First" />  
  3. <ListBoxItem Content="Second" />  
  4. <ListBoxItem Content="Third" />  
  5. </ListBox>  
                                                Listing 2

We can declare as many items inside the ListBox control. Now, replace the commented Pane Items with the following code:
  1. <ListBox SelectionMode="Single"   
  2.             SelectionChanged="ListBox_SelectionChanged">  
  3.     <ListBoxItem Name="Back" Background="Gray">  
  4.         <StackPanel Orientation="Horizontal">  
  5.             <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" Text="" />  
  6.             <TextBlock FontSize="24" Margin="10,0,0,0">Back</TextBlock>  
  7.         </StackPanel>  
  8.     </ListBoxItem>  
  9.     <ListBoxItem Name="Hamburger">  
  10.         <StackPanel Orientation="Horizontal">  
  11.             <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" Text="" />  
  12.             <TextBlock FontSize="24" Margin="10,0,0,0">Menu</TextBlock>  
  13.         </StackPanel>  
  14.     </ListBoxItem>  
  15.     <ListBoxItem Name="Home">  
  16.         <StackPanel Orientation="Horizontal">  
  17.             <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" Text="" />  
  18.             <TextBlock FontSize="24" Margin="10,0,0,0">Home</TextBlock>  
  19.         </StackPanel>  
  20.     </ListBoxItem>  
  21.     <ListBoxItem Name="Settings">  
  22.         <StackPanel Orientation="Horizontal">  
  23.             <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" Text="" />  
  24.             <TextBlock FontSize="24" Margin="10,0,0,0">Settings</TextBlock>  
  25.         </StackPanel>  
  26.     </ListBoxItem>  
  27. </ListBox>  
                                                Lisitn 3

Here we have taken a ListBox, and there are four List Box items like previously we had four Radio Buttons. Each item represents correspondent SplitView Pane item. Inside the ListBoxItem, there is a StackPanel to hold the icon and the text of the items. StackPanel arranges the TextBlocks in a same alignment. In first TextBlock, Font Family is Sagoe MDL2 Assets, which is the same as Character Map special character set, and Text is just the following character.

Map special characte
                                                               Figure 1

Adding Extra Pages

Now, create two blank pages named Page1.xaml and Page2.xaml. In Page1.xaml, change the code like the following.
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.    <TextBlock FontSize="36" Text="Home Page" Margin="12,17,0,17"/>  
  3. </Grid>  
                                                      Listing 4

Similarly, in Page2.xaml change the code like the following. 
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2. <TextBlock FontSize="36" Text="Settings Page" Margin="12,17,0,17"/>  
  3. </Grid>  
                                                         Listing 5

Handle events in code-behind MainPage.xaml.cs

The event handlers of the Split View’s Radio Buttons are as follows.
  1. private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  2. {  
  3.     if (Back.IsSelected)  
  4.     {  
  5.         if (MyFrame.CanGoBack)  
  6.         {  
  7.             MyFrame.GoBack();  
  8.         }  
  9.     }  
  10.     else if (Hamburger.IsSelected)  
  11.     {  
  12.         if (!this.SplitView.IsPaneOpen)  
  13.         {  
  14.             this.SplitView.IsPaneOpen = true;  
  15.         }  
  16.         else  
  17.         {  
  18.             this.SplitView.IsPaneOpen = false;  
  19.         }  
  20.     }  
  21.     else if (Home.IsSelected)  
  22.     {  
  23.         MyFrame.Navigate(typeof(Page1));  
  24.     }  
  25.     else if (Settings.IsSelected)  
  26.     {  
  27.         MyFrame.Navigate(typeof(Page2));  
  28.     }  
  29.     else  
  30.     {  
  31.         // do nothing  
  32.     }  
  33. }  
                                                                  Listing 6

If you notice, we do not have any extra Button control here, and it is just the SelectionChanged event of the ListBox control. Every ListBoxItem has a unique name, so we check whether it is selected or not. If so, we have made the frame navigate to other correspondent page. You can also see the previous article at http://bit.ly/1O626HP.

Running the Application

If you run the application, it will look like the following screenshot:

Running the Application
                                             Figure 2

Conclusion

ListBox is a common and useful control. You can use it for static data as well as some other data source. Hope this helps you to understand the basic implementation of ListBox in Universal Windows Application. Happy Coding!

Download the full source code at http://1drv.ms/1L7iqY6.


Similar Articles