Windows-8.1

How to work with ListBox selected item in Windows phone application

In this post I am going to talk about different techniques of accessing a Listbox Selected item.

When we populate a Listbox with data items we have two options

1. First is that we can use Listbox items directly

2. Second we can make the use of databinding.

 

When we use option 1 the Selected item is usually the Listbox item so we can access it easily and perform the desired manipulations on it.

But when we make use of Second option which is the most widely used option the SelectedItem is the type of the business object (data class) which we use when performing data binding

There are different methods of accessing the ListBox Selected item which will be explained below with the help of an example.

 

In the following example I shall be creating a Listbox of student names along with a button which shall display their marks along with their name in a Texblock given below the List.

The different steps that need to be followed are

 

1. Create a new project Listboxselecteditem.

2. Then open the MainPage.xaml and remove the XAML code for the StackPannel named TitlePanel and replace it with the following code so as to change the page name and application name .

 


 

 

3. Now add the following Xaml code in the Grid named ContentPanel so as to create a list box in the page having the name mylistbox and bind the target Textblock nameblock  with data source name property. It also adds two Textblocks having the name Studentnameblock and marksblock to display the name and marks of selected student.

 



 

4. Now open the page MainPage.xaml.cs.

5. Create a public class called Resultclass having the properties name and marks in the page globally.


 

6. Then add the following code consisting of two global arrays for sample data to be used inside listbox named mylistbox

 

7. After that make a sample list from the data given above with the help of the following code given below.

 


 


 

8. Then call the function makeasamplelist in the constructor of the MainPage.xaml.cs.

 


 

If you face any problems while understanding the above code you can refer to the following blog post to understand the basics of data binding.

http://www.windowsapptutorials.com/windows-phone/data-binding-in-windows-phone-app-one-way-binding/

 

9. Now to add the following code for the Listbox SelectionChanged Event handler in the MainPage.xaml.cs.

This code will execute whenever you make a selection from the Listbox.

 


10. The code for the marks button Click EventHandler is as following. Whenever you click on the Marks button of a any Listbox item this code will execute.

 



.

11. Now run the application. You will see the following List created in your application.

 


 

12. Now Select any Listbox item. Suppose we select the second item that is Fred. When you will Select it the values of Textblocks Studentnameblock and marksblock will change respectively.

 


 

13. When you click on Marks button given in front of the student name the corresponding student name and marks will be shown in the Textblocks given below the Listbox.

When you Click on the Marks button given in front of James the following output will be shown.

 


 

14. If hope this post will be helpful to you. Download the full project zip file from the link given below.

 

Download full project file Listboxselecteditem.zip

 

Chetan Gulati

Currently am doing B TECH in IT from Delhi Technological University. App development is my passion. Believe in quality rather than quantity.

  • Raoul

    This work also with a ListView?

    • Not tried truly..But i hope it would work..Just give it a try..

  • Mario Sanchez

    omg !! you are awesome !!!

  • Paulina Gawin

    Great! Thank you. I needed this for my class.

  • Lexi Mize

    Thank you thank you thank you. This is key!

    private void ButtonMyThingyFeature_Click(object sender, RoutedEventArgs e)
    {
    MyObjectThingy thing = (sender as Button).DataContext as MyObjectThingy;

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close