Data binding is a simple way to display data, and the connection or binding between a UI element and a data object allows the data to flow between the two. When a binding is established and the data changes, the corresponding UI element automatically displays the change.
How to bind a UI element to data
As you can see from the diagram above, each binding must specify a source and a target.
Where the source object can be any CLR object, including the target element itself and other UI elements. The target can be any DependencyProperty (dependent attribute) of the FrameworkElement.
The data-binding engine obtains the following from the binding object:
1) Source object and target object;
2 The direction of data flow;
(3) value converter;
For example, use C # code and XAML to bind the foreground property of a TextBox.
XAML Code:
<textboxx:name= "MyTextBox" text= "Text" foreground= "{Binding Brush1,
mode=oneway}"/>
C # code:
MyColors textcolor = new mycolors
();
TextColor. BRUSH1 = new SolidColorBrush
(colors.red);
Mytextbox.datacontext = TextColor;
Bindings are using {Binding ...} Syntax is created in XAML. The source is set in code by setting the DataContent property of the TextBox.
In addition, the data will be inherited. If we set the data context on a parent element, its child elements will use the same data. We can bind to a property of the source object by setting the Binding.path property.
Direction of data binding
Each binding contains a mode property that determines how and when the data flows.
Three kinds of bindings:
1) Onetime: bindings use source data to update targets at creation time.
2) OneWay: Binding will use source data to update the target (default mode) when it is created and when the data changes.
3) TwoWay: Binding updates both the target and the source when any one of the target and source changes occurs.