Universal Windows Platform - Data Binding

Data binding is a useful technique to populate data into your application. It’s really useful when you’ve massively structured code, and you’ve to handle a lot of data. It binds your whole bunch of data into a single control, such as ListView, ListBox, FlipView or it can be your custom control. XAML gives us the power to bind the data into these structural controls with less effort and in an efficient way.

So, let’s get started with Universal Windows Platform app development with a simple data binding technique.

Different Binding Modes

There are a number of different binding modes, as defined in the BindingMode enumeration:

  • TwoWay – Changes to the UI or model automatically update the other. This is used for editable forms or other interactive scenarios.

  • OneWay – Updates the UI when the model changes. This is appropriate for read-only controls populated from data.

  • OneTime – Updates the UI when the application starts or when the data context changes. This is used when the source data static/does not change.

  • OneWayToSource – Changes to the UI update the model.

  • Default – Uses the default mode for the particular binding target (UI control). This differs based on the control.

The Universal Windows Platform, introduces a new kind of binding is Compile Binding (x:Bind). Comparing traditional binding techniques, it is really efficient and also faster. If you have less amount of data, may be you won’t notice the actual change of execution time but when you’re working on vast amount of data, you will notice the actual differences. To start with our project, firstly, create a new blank application. Then, create a new folder and give it a name “Images”. I have used Images from Microsoft theme download site. Now, add these images into this folder.

Image
Figure 1

Designing UI

To design the UI element, we’ve used FlipView control. Because we want to show the images by using data binding technique. Paste the code into your main grid element.

  1. <FlipView Name="MyFlipView" BorderBrush="Black" BorderThickness="1" ItemsSource="{x:Bind Items}" xmlns:m="using:App2">  
  2.     <FlipView.ItemTemplate>  
  3.         <DataTemplate x:DataType="m:Images">  
  4.             <Grid>  
  5.                 <Image Source="{x:Bind images}" Stretch="Fill" />  
  6.                 <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">  
  7.                     <TextBlock Text="{x:Bind title}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" /> </Border>  
  8.             </Grid>  
  9.         </DataTemplate>  
  10.     </FlipView.ItemTemplate>  
  11. </FlipView>  
Listing 1

Here, the name of the FlipView control is “MyFlipView” and there are two main child element: Image and TextBox. TextBox control is wrapped by a Border control which give a little bit of effect in the UI.

Most importantly, if you notice we’ve bind the ItemSource by compile binding and that’s why we’ve to define DataType (m:Images) by its class name. Here Images is the name of the class which contains an image and string attribute.
  1. public class Images  
  2. {  
  3.    public BitmapImage images { getset; }  
  4.    public string title { getset; }  
  5. }  
Listing 2

Lastly, we bind the Image and TextBox with “images” and “title”. The backend code will be like the following code snippet.
  1. public ObservableCollection<Images> Items { getprivate set; } = new ObservableCollection<Images>();  
  2. …  
  3. BitmapImage[] bitmapImage = new BitmapImage[9];  
  4. bitmapImage[0] = new BitmapImage(new Uri("ms-appx:///Images/milkyway1.jpg", UriKind.RelativeOrAbsolute));  
  5. bitmapImage[1] = new BitmapImage(new Uri("ms-appx:///Images/milkyway2.jpg", UriKind.RelativeOrAbsolute));  
  6. bitmapImage[2] = new BitmapImage(new Uri("ms-appx:///Images/milkyway3.jpg", UriKind.RelativeOrAbsolute));  
  7. bitmapImage[3] = new BitmapImage(new Uri("ms-appx:///Images/milkyway4.jpg", UriKind.RelativeOrAbsolute));  
  8. bitmapImage[4] = new BitmapImage(new Uri("ms-appx:///Images/milkyway5.jpg", UriKind.RelativeOrAbsolute));  
  9. bitmapImage[5] = new BitmapImage(new Uri("ms-appx:///Images/milkyway6.jpg", UriKind.RelativeOrAbsolute));  
  10. bitmapImage[6] = new BitmapImage(new Uri("ms-appx:///Images/milkyway7.jpg", UriKind.RelativeOrAbsolute));  
  11. bitmapImage[7] = new BitmapImage(new Uri("ms-appx:///Images/milkyway8.jpg", UriKind.RelativeOrAbsolute));  
  12. bitmapImage[8] = new BitmapImage(new Uri("ms-appx:///Images/milkyway9.jpg", UriKind.RelativeOrAbsolute));  
  13.   
  14. string[] title = new string[] { "Milkyway 1""Milkyway 2""Milkyway 3""Milkyway 4""Milkyway 5""Milkyway 6""Milkyway 7""Milkyway 8""Milkyway 9" };  
  15.   
  16. for (int i = 0; i < 9; i++)  
  17. {  
  18.     this.Items.Add(new Images { images = bitmapImage[i], title = title[i] });  
  19. }  
  20.   
  21. MyFlipView.ItemsSource = Items;  
Listing 3

Here, we declare a list item named “Items” and assign it to “MyFlipView” control. We’ve added the image and title array to the collection list by using a for loop.

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

Milkeyway
Figure 2

Another way to bind is the traditional way that’s we did before in Windows Phone 8.1 and later. We’ve changed a little bit to our XAML code.
  1. <FlipView Name="MyFlipView" BorderBrush="Black" BorderThickness="1" ItemsSource="{Binding Items}">  
  2.     <FlipView.ItemTemplate>  
  3.         <DataTemplate>  
  4.             <Grid>  
  5.                 <Image Source="{Binding images}" Stretch="UniformToFill" />  
  6.                 <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">  
  7.                     <TextBlock Text="{Binding title}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" /> </Border>  
  8.             </Grid>  
  9.         </DataTemplate>  
  10.     </FlipView.ItemTemplate>  
  11. </FlipView>  
Listing 4

If we compare two binding techniques, we can see that compile binding is much faster than normal binding.

Compile Binding
Figure 3: Compile Binding

Here the memory process is 60 bytes. If you look at the normal binding, the memory process is 70 bytes.

Normal Binding
Figure 4:
Normal Binding

Closure

Hopefully, you have understood the binding techniques in Universal Windows Platform app development. You can apply this procedure in any form of application and make an efficient way to serve you data. Happy coding!

You can download the full source code, here.

 


Similar Articles