Introduction
We all know WPF is UI designing framework that has an enhanced way of displaying and manipulating data.
The concept is known as Data Binding, we can get data from 'N' number sources such as API, entity framework any other ORM tools, or static data.
Here, data binding is a layer between UI elements & data.
It shows dynamic data changes: meaning if the data source is updated so is your UI.
In WPF, there are 4 types of binding
- One-Way Binding
- Two-Way Binding
- One-Way To Source
- One_Time Binding
- Default
Let's learn them one by one.
One-Way Data-binding
One way allows changes from the source-destination meaning, data can only flow from one direction.
Create a WPF app with the following controls.
A grid with 2 labels & 2 textboxes in XAML might look something like this.
<Window x:Class="A.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:A"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="500">
<Window.Resources>
<Thickness x:Key="MarginTop">0 5 0 0</Thickness>
</Window.Resources>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Content="Source" Margin="0 30 0 0"/>
<TextBox x:Name="SourceTextBox" Width="150" Height="25" Margin="0 30 0 0" Grid.Column="1"/>
<Label Content="Destination" Margin="0 5 0 0" Grid.Row="1"/>
<TextBox x:Name="DestinationTextBox" Text="{Binding ElementName=SourceTextBox, Path=Text, Mode=OneWay}"
Margin="{StaticResource MarginTop}" Width="150" Height="25" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Window>
The first textbox will take input and the second textbox will display the same text that you entered in the first textbox.
the second textbox will update itself with every single keystroke you entered in the first textbox.
As you can see in the second textbox which is named DestinationTextBox we have Text Property: which is bound with a first textbox(SourceTextBox) & has a set
Mode = OneWay
This is how the output would look like: Note: watch the complete gif to understand One-way binding's behavior.

Explanation
As you can see, whatever I type in the source textbox it gets reflected in the destination textbox. But it doesn't work vice-versa. because our destination textbox has one-way binding. it will not let the source know if it has updated itself.
Two-Way Binding
Data can flow bi-directionally, from source to destination and from destination to source.
Keep the same example, but make changes in DestinationTextBox as follows.
<TextBox x:Name="DestinationTextBox"
Text="{Binding ElementName=SourceTextBox, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="{StaticResource MarginTop}"
Width="150"
Height="25"
Grid.Column="1"
Grid.Row="1"/>




Hans HuijnenPosted Jan 2, 2021, 7:26 AM
In the the title it's "its"