DataBinding In WPF

DataBinding  is a mechanism in WPF applications that provides a simple and easy way for applications to display and interact with the data. It allows the flow of data between UI and business model. Any modification done on data in your business model after binding is done, will automatically reflect to the UI, and vice versa.

Binding can be one directional or bidirectional. The source of databinding can be a normal .NET property or Dependency property, however the target property must be a Dependency property.

For making binding work properly, both sides of the property must provide a change in notification which will tell the binding to update the target value. In normal .NET properties, it can be achieved by using INotifyPorpertyChanged interface. And for Dependency properties, it is done by PropertyChanged callback of property metadata.

Databinding is achieved in XAML by using Binding mark-up extension i.e. {Binding}.

Eaxmple

  1. <Window.Resources>  
  2.     <local:Employee x:Key="MyEmployee" EmployeeNumber="123" FirstName="John" LastName="Doe" Department="Product Development" Title="QA Manager" /> </Window.Resources>  
  3. <Grid DataContext="{StaticResource MyEmployee}">  
  4.     <TextBox Text="{Binding Path=EmployeeNumber}"></TextBox>  
  5.     <TextBox Text="{Binding Path=FirstName}"></TextBox>  
  6.     <TextBox Text="{Binding Path=LastName}" />  
  7.     <TextBox Text="{Binding Path=Title}"></TextBox>  
  8.     <TextBox Text="{Binding Path=Department}" />  
  9. </Grid>  

DataContext

Datacontext property is used for setting the data to UI. If you do not explicitly define the source of binding, then it takes data context as default.

Types of Binding

  • OneWay
  • TwoWay
  • OneWayToSource
  • OneTime

One Way

In this type of binding, data is bound to its source; i.e., business model to its target; i.e. UI.

In xaml file

  1. <Window x:Class="WPFDataBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFDataBinding" mc:Ignorable="d" Title="MainWindow" Height="350" Width="604">  
  2.     <Grid>  
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="Auto" />  
  5.             <RowDefinition Height="Auto" />  
  6.             <RowDefinition Height="*" /> </Grid.RowDefinitions>  
  7.         <Grid.ColumnDefinitions>  
  8.             <ColumnDefinition Width="Auto" />  
  9.             <ColumnDefinition Width="200" /> </Grid.ColumnDefinitions>  
  10.         <Label Name="nameLabel" Margin="2">_Name:</Label>  
  11.         <TextBox Name="nameText" Grid.Column="1" Margin="2" Text="{Binding Name, Mode = OneWay}" />  
  12.         <Label Name="ageLabel" Margin="2" Grid.Row="1">_Age:</Label>  
  13.         <TextBox Name="ageText" Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Age, Mode = OneWay}" />  
  14.         <StackPanel Grid.Row="2" Grid.ColumnSpan="2">  
  15.             <Button Content="_Show..." Click="Button_Click" /> </StackPanel>  
  16.     </Grid>  
  17. </Window>  

Now, in xaml.cs file,

  1. using System.Windows;  
  2. namespace WPFDataBinding {  
  3.     public partial class MainWindow: Window {  
  4.         Person person = new Person {  
  5.             Name = "Salman", Age = 26  
  6.         };  
  7.         public MainWindow() {  
  8.             InitializeComponent();  
  9.             this.DataContext = person;  
  10.         }  
  11.         private void Button_Click(object sender, RoutedEventArgs e) {  
  12.             string message = person.Name + " is " + person.Age;  
  13.             MessageBox.Show(message);  
  14.         }  
  15.     }  
  16.     public class Person {  
  17.         private string nameValue;  
  18.         public string Name {  
  19.             get {  
  20.                 return nameValue;  
  21.             }  
  22.             set {  
  23.                 nameValue = value;  
  24.             }  
  25.         }  
  26.         private double ageValue;  
  27.         public double Age {  
  28.             get {  
  29.                 return ageValue;  
  30.             }  
  31.             set {  
  32.                 if (value != ageValue) {  
  33.                     ageValue = value;  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38. }  

Two Way

In this, data is getting updated from both sides; i.e., source and target. If there is any change from UI, it updates the business model, and vice versa.

  1. <Window x:Class="WPFDataBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFDataBinding" mc:Ignorable="d" Title="MainWindow" Height="350" Width="604">  
  2.     <Grid>  
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="Auto" />  
  5.             <RowDefinition Height="Auto" />  
  6.             <RowDefinition Height="*" /> </Grid.RowDefinitions>  
  7.         <Grid.ColumnDefinitions>  
  8.             <ColumnDefinition Width="Auto" />  
  9.             <ColumnDefinition Width="200" /> </Grid.ColumnDefinitions>  
  10.         <Label Name="nameLabel" Margin="2">_Name:</Label>  
  11.         <TextBox Name="nameText" Grid.Column="1" Margin="2" Text="{Binding Name, Mode = TwoWay}" />  
  12.         <Label Name="ageLabel" Margin="2" Grid.Row="1">_Age:</Label>  
  13.         <TextBox Name="ageText" Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Age, Mode = TwoWay}" />  
  14.         <StackPanel Grid.Row="2" Grid.ColumnSpan="2">  
  15.             <Button Content="_Show..." Click="Button_Click" /> </StackPanel>  
  16.     </Grid>  
  17. </Window>  

OneWayToSource

OneWayToSource is the reverse of OneWay binding; it updates the source property when the target property changes.

OneTime

This has the same behaviour as OneWay except it will only update the user interface one time. This should be your default choice for binding.