wpf
  1. wpf-data-binding

Data Binding - (WPF)

Data binding is a powerful feature of Windows Presentation Foundation (WPF) that allows you to bind data from a data source to a user interface element, such as a textbox or listbox. This enables the user interface to stay synchronized with the data, so changes to one are reflected in the other.

Syntax

To use data binding in WPF, you can use a binding expression in XAML to bind a data source to a user interface element. Here is an example of how to use data binding in WPF:

<TextBox Text="{Binding Path=MyProperty}" />

Example

Here is an example of how to use 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="MainWindow" Height="350" Width="525">
  <Grid>
    <TextBox Text="{Binding Path=MyProperty}" />
  </Grid>
</Window>
using System.ComponentModel;

namespace MyNamespace
{
  public class MyViewModel : INotifyPropertyChanged
  {
    private string _myProperty;

    public event PropertyChangedEventHandler PropertyChanged;

    public string MyProperty
    {
      get { return _myProperty; }
      set
      {
        if (_myProperty != value)
        {
          _myProperty = value;
          OnPropertyChanged(nameof(MyProperty));
        }
      }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
  }
}
using System.Windows;

namespace MyNamespace
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      DataContext = new MyViewModel() { MyProperty = "Hello, world!" };
    }
  }
}

Output

When you run the WPF application that has data binding enabled, you should see a textbox that displays the value of the MyProperty property in the view-model. If you change the value of the textbox, the view-model's MyProperty property should also change.

Explanation

In the example code, we create a simple WPF application that uses data binding to connect a view-model to a textbox in the user interface. We define the view-model as a C# class that implements the INotifyPropertyChanged interface, which provides the ability to notify the user interface when a property value changes. We then set the DataContext property of the WPF window to an instance of the view-model, which provides the data source for the data binding expression in the textbox's Text property.

Use

Data binding is a powerful tool for creating dynamic and data-driven user interfaces in WPF. By binding data from a view-model or other data source to user interface elements, you can create responsive and interactive applications.

Important Points

  • Data binding is a powerful feature of WPF that allows you to bind data from a data source to a user interface element, such as a textbox or listbox.
  • Data binding can be used to keep the user interface synchronized with the data source, so changes to one are reflected in the other.
  • The INotifyPropertyChanged interface can be used to notify the user interface when a property value changes.

Summary

In this page, we discussed how to use data binding in WPF. We covered the syntax, example, output, explanation, use, important points, and summary of using data binding in a WPF application. Data binding is an essential tool for creating dynamic and data-driven user interfaces in WPF, and enables the user interface to stay synchronized with the data source. By using data binding, you can create responsive and interactive applications.

Published on: