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" 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:JWPF" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <TextBox x:Name="SampleTextBox" HorizontalAlignment="Left" Margin="320,174,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> </Grid> </Window>
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() { InitializeComponent(); 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