Silverlight ListBox Control

Introduction

 
Today let us discuss something about the listbox control with some case scenario. Let us suppose that we want to display a list of items in a listbox with a button for each list box so the very obvious solution to the problem is the use of a data template or the other way can do it is to create your own user control and place inside the list box as a list box item. Let's see how we can use both the data template as well as custom user control to meet the criteria.
 
Setting Up Silverlight Project
  1. Let's create a Silverlight project; let's name it UseOfListBox.
  2. Then we will place a ListBox on the XAML and let's name it personListBox.
  3. Let's create an item template of the list box which will look like the following:
    1. <ListBox x:Name="personListBox" Height="241" HorizontalAlignment="Left" Margin="42,24,0,0" VerticalAlignment="Top" Width="280">  
    2.     <ListBox.ItemTemplate>  
    3.         <DataTemplate>  
    4.             <StackPanel>  
    5.                 <TextBlock FontFamily="Verdana" Text="{Binding ID}" Margin="0,0,5,0"></TextBlock>  
    6.                 <TextBlock FontFamily="Verdana" Text="{Binding Name}" Margin="0,0,5,0"></TextBlock>  
    7.                 <TextBlock FontFamily="Verdana" Text="{Binding Designation}" Margin="0,0,5,0"></TextBlock>  
    8.             </StackPanel>  
    9.         </DataTemplate>  
    10.     </ListBox.ItemTemplate>  
    So this is the item template that will represent the data.
     
  4. Create a collection of objects to bind to this list box. So let's create a class such as:
    1. public class Person {  
    2.     public int ID { getset; }  
    3.     public string Name { getset; }  
    4.     public string Designation { getset; }  
    5. }  
  5. Finally bind the data to the list box like this:
    1. List < Person > personList = new List < Person > () {  
    2.     new Person { ID = 1, Name = "Mukesh", Designation = "Developer" },  
    3.     new Person { ID = 2, Name = "Rakesh", Designation = "Developer" },  
    4.     new Person { ID = 3, Name = "Rajesh", Designation = "Developer" },  
    5.     new Person { ID = 4, Name = "Dinesh", Designation = "Developer" },  
    6.     new Person { ID = 5, Name = "Rohit", Designation = "Developer" },  
    7.     new Person { ID = 6, Name = "Mohit", Designation = "Developer" }  
    8. };  
    9. personListBox.ItemsSource = personList;  
So the output will look something like:
 
UsrCntrlLstVw1.jpg
 
This was simple and easy.
 
But there is another way we can create a custom user control which will be integrated as a ListBoxItem. So let's move in that way:
 
Let's create another page; let's say CustomListBox.xaml. Exactly in the same way, we will place the listBox with the same name. After that, let's create a UserControl named personControl which will look like as follows:
  1. <UserControl x:Class="UseOfListBox.PersonControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Height="Auto" Width="Auto">  
  2.     <Grid x:Name="LayoutRoot" Background="White">  
  3.         <StackPanel DataContext="{Binding}">  
  4.             <TextBlock FontFamily="Verdana" Text="{Binding ID}" Margin="0,0,5,0"></TextBlock>  
  5.             <TextBlock FontFamily="Verdana" Text="{Binding Name}" Margin="0,0,5,0"></TextBlock>  
  6.             <TextBlock FontFamily="Verdana" Text="{Binding Designation}" Margin="0,0,5,0"></TextBlock>  
  7.         </StackPanel>  
  8.     </Grid>  
  9. </UserControl>  
The very important part of this user control is DataContext="{Binding}" which will help it to bind from the parent control. And then we will do a final change in the listbox item template and it will look like:
  1. <ListBox.ItemTemplate>  
  2.                <DataTemplate>  
  3.                    <CustomControl:PersonControl  />  
  4.                </DataTemplate>  
  5. </ListBox.ItemTemplate>  
Add the namespace xmlns:CustomControl="clr-namespace:UseOfListBox".
 
And finally, bind the data:
  1. List < Person > personList = new List < Person > () {  
  2.     new Person { ID = 1, Name = "Mukesh", Designation = "Developer" },  
  3.     new Person { ID = 2, Name = "Rakesh", Designation = "Developer" },  
  4.     new Person { ID = 3, Name = "Rajesh", Designation = "Developer" },  
  5.     new Person { ID = 4, Name = "Dinesh", Designation = "Developer" },  
  6.     new Person { ID = 5, Name = "Rohit", Designation = "Developer" },  
  7.     new Person { ID = 6, Name = "Mohit", Designation = "Developer" }  
  8. };  
  9.   
  10. personListBox.ItemsSource = personList;  
And the output will be exactly the same.
 
UsrCntrlLstVw2.jpg
 
Let's go a bit into depth about that. Let us say we have a requirement that we want a button to be the part of the listbox item and on its click event, we will show the Name of the selected person in a message box from the parent page. So let's add a button to the custom control and then XAML will look like the following:
  1. <Grid x:Name="LayoutRoot" Background="White">  
  2.     <StackPanel DataContext="{Binding}">  
  3.         <TextBlock FontFamily="Verdana" Text="{Binding ID}"></TextBlock>  
  4.         <TextBlock FontFamily="Verdana" Text="{Binding Name}"></TextBlock>  
  5.         <TextBlock FontFamily="Verdana" Text="{Binding Designation}"></TextBlock>  
  6.         <Button Width="100" Height="30" Content="Detail" x:Name="detailButton" />  
  7.     </StackPanel>  
  8. </Grid>  
And now we will take advantage of the event handler and create an event handler which will track the button click. So in the code behind of the user control we will add the following:
  1. public event EventHandler buttonClicked;  
  2. public PersonControl() {  
  3.     InitializeComponent();  
  4.     detailButton.Click += new RoutedEventHandler(DetailButton_Click);  
  5. }  
  6.   
  7. void DetailButton_Click(object sender, RoutedEventArgs e) {  
  8.     if (buttonClicked != null) {  
  9.         buttonClicked(this, e);  
  10.     }  
  11. }  
Finally we have to receive the information in the parent page (that is CustomListBox.xaml) so we will do a bit of modification. And then the data template for the ListBox will look like:
  1. <DataTemplate>  
  2.     <CustomControl:PersonControl buttonClicked="ButtonClickedUp" />  
  3. </DataTemplate>  
And the event will do the following :
  1. void ButtonClickedUp(object sender, EventArgs e) {  
  2.     Person selectedPerson = (Person)((PersonControl) sender).DataContext;  
  3.     MessageBox.Show(selectedPerson.Name);  
  4. }  
So the final output will be:
 
UsrCntrlLstVw3.jpg
 
And when clicking on the button there will be a pop up like these:
 
UsrCntrlLstVw4.jpg
 
Hope this will help !!!


Similar Articles