Two Way Data Binding - (WPF)
Two way data binding is a powerful feature of Windows Presentation Foundation (WPF) that allows you to create dynamic and responsive user interfaces. Two way data binding allows changes made to an object in the user interface to be reflected in the underlying data object, and vice versa. This can simplify the development of complex user interfaces, and make your WPF applications more interactive and intuitive.
Syntax
Two way data binding in WPF is achieved using the Binding
class. Here is an example of how to use two way data binding in WPF:
<TextBox Text="{Binding Path=UserName, Mode=TwoWay}" />
Example
Here's an example of how to use two way data binding in WPF:
<Window x:Class="MyNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Application" Height="350" Width="525">
<Grid>
<StackPanel>
<Label Content="User Name:" />
<TextBox Text="{Binding Path=UserName, Mode=TwoWay}" />
<Button Content="Save" Click="SaveButton_Click" />
</StackPanel>
</Grid>
</Window>
using System.ComponentModel;
using System.Windows;
namespace MyNamespace
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _userName;
public MainWindow()
{
InitializeComponent();
DataContext = this;
UserName = "John Doe";
}
public string UserName
{
get { return _userName; }
set
{
if (value != _userName)
{
_userName = value;
OnPropertyChanged("UserName");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// Save the user name
}
}
}
Output
When you run the WPF application that has two way data binding enabled, you should see a user interface that allows you to enter and save a user name. When you enter a user name in the text box and click the save button, the user name should be saved and reflected back in the text box.
Explanation
In the example code, we define a simple WPF user interface that has a label, a text box, and a button. We enable two way data binding for the text box by setting the Text
property to a binding expression that references the UserName
property of the window. We then define the UserName
property of the window as a string property that implements the INotifyPropertyChanged
interface. This allows changes made to the UserName
property in the user interface to be propagated back to the window, and vice versa. When the user clicks the save button, we can save the user name to a database or file.
Use
Two way data binding is a powerful feature of WPF that can simplify the development of complex user interfaces. By allowing changes made to objects in the user interface to be reflected in the underlying data object, and vice versa, you can create dynamic and responsive user interfaces that are easy to use and maintain.
Important Points
- Two way data binding in WPF is achieved using the
Binding
class. - The
Mode
property of theBinding
class can be set toTwoWay
to enable two way data binding. - Two way data binding requires that the underlying data object implement the
INotifyPropertyChanged
interface.
Summary
In this page, we discussed how to use two way data binding in WPF. We covered the syntax, example, output, explanation, use, important points, and summary of using two way data binding with a WPF user interface. Two way data binding is a powerful feature of WPF that can simplify the development of complex user interfaces by allowing changes made to objects in the user interface to be reflected in the underlying data object, and vice versa. By implementing the INotifyPropertyChanged
interface, you can enable two way data binding in your WPF applications.