Data binding means populating some data from one object to another object.The first object which provides data is called the source object.The second object which receives data from the first object is called the target object.Data binding acts as a bridge to pass data from source object to destination object.
The source object can be any object.Source object can be a normal CLR object.The property of destination object being data bound should be a dependency property.
For data binding in WPF you use the binding markup extension.
If you you want to bind the Text property of a text box with the Name property of the source object then you use the binding markup extension as:
<TextBox text="{Binding Name}" > </TextBox>
We are using binding markup extension here.The binding markup extension is one of the several markup extensions we can use in XAML.The markup extensions are written in curly braces.When we are using binding markup extension we write binding followed by the name of the property we are binding to.
If you want to bind the Text property of one text box with text property of another textbox then you use binding markup extension as:
<TextBox text="{Binding ElementName=txtPerson, Path=text}" > </TextBox>
Binding Modes
If you remember data binding is used to bind two objects ,then you may realize that data can flow in different ways between the source and target.For example data can flow from source to target or from target to source.Binding modes are used to specify how this flow of data happens between the source and target.
There are four different types of binding modes.
- In Oneway binding data only target property is updated whenever the source property changes.
- In TwoWay binding both the target and source properties are updated when either of them is updated.
- OneTime is similar to Onewway ,except that the binding happens only one time initially.After that the changes in the source property are not updated in the target.
- OneWayToSource is the opposite of OneWay
These binding modes are defined by the BindingMode enumeration in the System.Windows.Data namespace.
Leave a Reply