C# WPF Tutorial – Two Way Data Binding

C# WPF Tutorial – Two Way Data Binding

C# WPF allows you two-way data binding, which means you can easily access or modify data either in xaml or in the C# file. Each UI component which you declare in C# or in XAML is equally available in the opposite file.

C# WPF Tutorial – Basic Understanding

This means if you create a textbox in xaml you can access that via name in C# and if you create a dynamic textbox in C#, WPF will automatically generate equivalent xaml inside the xaml file.

<Window x:Class="JWPF.MainWindow"
Title="MainWindow" Height="450" Width="800">
<TextBox x:Name="SampleTextBox" HorizontalAlignment="Left" Margin="320,174,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

In the above code, I just added the textbox having the name “SampleTextBox”. If I want to modify the value of this textbox after some logic, I have to write the code in the C# file to do that, as you can observe following code.

public partial class MainWindow : Window
public MainWindow()
SampleTextBox.Text = "C# WPF Tutorial";

This is how two-way data binding is working from xaml to c#

No one can explain better than the creator of the concept, such as in this case Microsoft well explained their technology concept of data binding here.

There is much more to understand but can’t explain in a single article, so for sake of understanding, I must let you know about events also which are equally important as concepts. Events are special triggers which occur in certain cases, such as if you want to update the value of any textbox when the button is clicked, so a click event will be triggered in which you will write the logic to update values.

From HERE you can learn more about data binding with actual examples.

For more clarification, you can watch my video based on WPF Two Way Data Binding