Data Binding in Windows Phone App

This article shows how to bind data in a Windows Phone app.

Data Binding means that we can set multiple lines of data into Toolbox elements. You can't write multiple lines in an XAML file as a string of the Element property, it looks bad. Therefore we always use binding in Windows Phones, like for a Button the property is “Content={Binding btntxt}” and in a TextView the property is “Text={Binding txt}”.

So “btntxt” and “txt” binds data from C# code and puts that data into a property as a string value. I'm telling you how to do that.

First create a Windows Phone Blank App.

Windows Phone Blank App

Now open a MainPage.xaml file.

  1. <Grid>  
  2.    <Button x:Name="btn" Content="{Binding btntxt}" HorizontalAlignment="Left" Margin="36,164,0,0" VerticalAlignment="Top" Height="115" Width="309" Click="btn_Click" />  
  3.   
  4. </Grid>  
Now open a MainPage.xaml.cs file and create another class in the MainPage class and see the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using Windows.Foundation;  
  7. using Windows.Foundation.Collections;  
  8. using Windows.UI.Xaml;  
  9. using Windows.UI.Xaml.Controls;  
  10. using Windows.UI.Xaml.Controls.Primitives;  
  11. using Windows.UI.Xaml.Data;  
  12. using Windows.UI.Xaml.Input;  
  13. using Windows.UI.Xaml.Media;  
  14. using Windows.UI.Xaml.Navigation;  
  15.   
  16. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641  
  17.   
  18. namespace App3 {  
  19.     /// <summary>  
  20.     /// An empty page that can be used on its own or navigated to within a Frame.  
  21.     /// </summary>  
  22.     public sealed partial class MainPage: Page {  
  23.         public MainPage() {  
  24.             this.InitializeComponent();  
  25.         }  
  26.         public class txt {  
  27.             public string btntxt {  
  28.                 get;  
  29.                 set;  
  30.             }  
  31.         }  
  32.         private void btn_Click(object sender, RoutedEventArgs e) {  
  33.             txt t = new txt();  
  34.             t.btntxt = "You've Clicked on Button";  
  35.             btn.DataContext = t;  
  36.         }  
  37.     }  
  38. }  
Output

Before click on the button.

Output

The click on the button.

Click on Button
Again for the TextBlock and TextBox.

Now open the MainPage.xaml file and drag and drop one TextBox and one TextBlock onto the Grid Layout so it looks as in the following:
  1. <Grid>  
  2.     <TextBlock x:Name="txtb" HorizontalAlignment="Left" Margin="106,240,0,0" TextWrapping="Wrap" FontSize="30" Text="{Binding txtblock}" VerticalAlignment="Top"/>  
  3.     <TextBox x:Name="txtbo" HorizontalAlignment="Left" Margin="148,127,0,0" TextWrapping="Wrap" Text="{Binding txtBox}" VerticalAlignment="Top" Width="183" Loaded="txtbo_Loaded"/>  
  4.     <Button x:Name="btn" Content="Submit" Margin="106,271,0,312" Click="btn_Click" />  
  5. </Grid>  
The preceding code Text={Binding txtblock} and Text={Binding txtBox} does the data binding.

Open the MainPage.xaml.cs file and create again a new class in the MainPage class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using Windows.Foundation;  
  7. using Windows.Foundation.Collections;  
  8. using Windows.UI.Xaml;  
  9. using Windows.UI.Xaml.Controls;  
  10. using Windows.UI.Xaml.Controls.Primitives;  
  11. using Windows.UI.Xaml.Data;  
  12. using Windows.UI.Xaml.Input;  
  13. using Windows.UI.Xaml.Media;  
  14. using Windows.UI.Xaml.Navigation;  
  15.   
  16. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641  
  17.   
  18. namespace App3 {  
  19.     /// <summary>  
  20.     /// An empty page that can be used on its own or navigated to within a Frame.  
  21.     /// </summary>  
  22.     public sealed partial class MainPage: Page {  
  23.         public MainPage() {  
  24.             this.InitializeComponent();  
  25.         }  
  26.         public class textdemo {  
  27.             public string txtblock {  
  28.                 get;  
  29.                 set;  
  30.             }  
  31.             public string txtBox {  
  32.                 get;  
  33.                 set;  
  34.             }  
  35.         }  
  36.         private void btn_Click(object sender, RoutedEventArgs e) {  
  37.             textdemo t = new textdemo();  
  38.             t.txtblock = "Your Name is :" + txtbo.Text;  
  39.             txtb.DataContext = t;  
  40.         }  
  41.         private void txtbo_Loaded(object sender, RoutedEventArgs e) {  
  42.             textdemo t = new textdemo();  
  43.             t.txtBox = "Enter Your Name";  
  44.             txtbo.DataContext = t;  
  45.         }  
  46.     }  
  47. }  
Output

Before click on the button.

Before Click on Button

Then click on the button and write your name in the TextBox as in the following:

After Click On Button

Thank you.