Working With ListBox Control In Universal Window App

Prerequisites

  • Visual Studio 2015

Now, let's get started with the following steps:

Step 1 - Create Windows Universal Project

Open Visual Studio 2015 and click File -> New -> Project for New Universal App.

Project

Step 2 - Giving the Project Name

Then, New Project window will open. There you can select Installed -> Template -> Visual C# -> Windows -> Universal and select a Blank App (Universal Windows).

Type Project Name as ListBoxApp and click OK.

Blank App

Step 3 - Setting the platform Versions

Here, we choose the Target Version and Minimum Version for our Universal Windows application and click OK.

Versions

Step 4 - Choose Designer Window

Now, we go to the Solution Explorer and open the MainPage.xaml for design.

Solution Explorer

Step 5 - Designing the App

In the MainPage.xaml designing page, drag the ListBox button control from the tool box and change the name as listboxcontrol. Then, go to the Properties window and click items.

List box button

Next, choose the ListBox item and click Add.

ListBox item

Next, choose Common -> Content from the properties window and change the item name.

properties

Likewise, you add the required ListBox items and click OK.

add

Then, add the Textblock control and change the name as textblockcontrol.

Textblock

Step 6 - Add the Coding

To add the coding, double click on the ListBox. Then, add the mentioned source code.

coding

  1. private void listboxcontrol_SelectionChanged(object sender, SelectionChangedEventArgs e)   
  2. {  
  3.     var selected = listboxcontrol.Items.Cast < ListBoxItem > ().Where(p => p.IsSelected).Select(t => t.Content.ToString()).ToArray();  
  4.     textblockcontrol.Text = "The selected List Box items are " + string.Join(", ", selected);  
  5. }  
Step 7 - Run the Application

Now, we are ready to run our Project. So, click Local Machine for running the Application.

Application

Output

Output

Output

Output

Conclusion

I hope you understood ListBox button control in Universal Windows, and how to run it. 

Up Next
    Ebook Download
    View all
    Learn
    View all