WPF Data Binding Basics


In this article we will discuss about what is Data Binding in wpf? How Binding Works? Details of Bindings Properties? What is role of INotifyPropertyChanged with bindings? What is need of Converters with the Data Bindings. Mode in Data Bindings.

Introduction



As per me binding makes life easy, wpf data binding providing a common way to bind data with UI controls with different data sources. That means we can bind a control as per their nature like drop down can be bind to list of objects and a string can be bind to text block or textbox control and xml data source can be bind to a tree control. So one by one we will discuss about all the things discussed in summary section of this article with example.

What is Data Binding?



Data binding is a way to connect UI controls with the real time objects or artefacts like customer, product, order etc..

<TextBlock Text="{Binding Path=CustomerName }"/>

Public Class Customer : INotifyPropertyChanged
{
public int ID { get; set; }
public string CustomerName { get; set; //raise on property change}
public event PropertyChangedEventHandler PropertyChanged;
}


How Binding Works?


In wpf data binding there is three important thing i.e. Binding object , Binding property and Binding source in above example TextBlock is target object and Text is Target Property (Must be a Dependency Property and not be Read Only Property) and finally source is Customer object.

In above case the how binding will work user will open the application and our main ui will load which is configured at app.xmal in the wpf project and then if the binding mode is set to one way that means if any changes made to text block will not affect in the object whereas if the binding mode is set to two way then through INotifiyPropertyChanged event will help to notify the client about the change in object so any place changes are made will reflect to both the places.

Details of Binding Property



There are lot of binding properties vary as per the control so we will discuss some common property which is generally used.

Converter: Converter is required in case when the binding property and binding object having different type or data type like Control Visibility is of Visibility type but in real scenario we will bind this with Boolean variable or property i.e. why wpf will use converter which is declared by us to convert data type details.

IsAsync: When we are dealing with asynchronously values or object will get value asynchronously then we are using this property.

Path: This is used to specify the property of our object and wpf engine to map the object with target property.

RelativeSource: This is used to specify the location of target control.

UpdateSourceTrigger: This property only related to binding mode two way and one way to source.

<TextBox Name="txtBox" Text="{Binding Path=CustomerName, UpdateSourceTrigger= PropertyChanged}" />\>


Note : This is initial to wpf data bindings in more article I will discuss about the different kind of data binding in WPF.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: