Data Binding In XAML UWP Using ComboBox

Let's discuss each.

What a ComboBox Control is -

Combobox is a control that enables a user to select one item or option among multiple ones. Combobox looks like the following in open and closed forms.

ComboBox Data Binding
Open form,

ComboBox Data Binding

Use of ComboBox Control

This control has a lot of uses in retail systems and other software where we want to let our end-user select one item from multiple options.

Binding of ComboBox Control

In this control, the data (like you just saw in the above image) is not static in professional development. In real time, case studies are needed to populate a combobox from any data source. This process of loading a control with some dynamic data is called Binding of Control with Data.

Example

Let's make a simple UI first and place a combobox on board. Make a UWP Empty Project in Visual Studio.

ComboBox Data Binding

Open MainPage.cs form Solution Explorer.

ComboBox Data Binding

Put this XAML code to make the UI.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.     <StackPanel Orientation="Horizontal" Margin="20">  
  3.         <TextBlock>Select Student</TextBlock>  
  4.         <ComboBox Name="ComboxBox1" ItemsSource="{x:Bind StuList}" Width="150" Margin="10,0"> </ComboBox>  
  5.     </StackPanel>  
  6. </Grid>   

Explanation of UI

We have placed two simple controls -

  • TextBlock (Contain some text only)
  • ComboBox (It has a name and an Itemsource Property)

Itemsource property is placed in an XAML Markup Extension of {x:Bind} that takes a data path. In our case, it is simply the name of object which contains the data to populate.

Code behind of UI

Now, we need to make a simple class of students. Like this. 

  1. public class Students {  
  2.     public int ID {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public override string ToString() {  
  11.         return this.Name;  
  12.     }  
  13. }  

Add this class to your project.

  • Now, open the code behind file of the UI.

    ComboBox Data Binding

  • Make a list of students and put all the code in Constructor so that whenever you run the application, the data will be instantiated automatically.

  • Make a list of students object in MainPage.xaml.cs Class code behind.

And now, populate the list in constructor.

  1. public sealed partial class MainPage: Page {  
  2.         List < Students > StuList = new List < Students > ();  
  3.         public MainPage() {  
  4.             this.InitializeComponent();  
  5.             StuList.Add(new Students() {  
  6.                 ID = 1, Name = "Ammar1"  
  7.             });  
  8.             StuList.Add(new Students() {  
  9.                 ID = 2, Name = "Ammar2"  
  10.             });  
  11.             StuList.Add(new Students() {  
  12.                 ID = 3, Name = "Ammar3"  
  13.             });  
  14.             StuList.Add(new Students() {  
  15.                 ID = 4, Name = "Ammar4"  
  16.             });  
  17.             StuList.Add(new Students() {  
  18.                 ID = 5, Name = "Ammar5"  
  19.             });  
  20.         }   
Now, run the project. You'll see the bind data.

ComboBox Data Binding

Up Next
    Ebook Download
    View all
    Learn
    View all