Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are data bindings applied?

At what time of object lifecycle are bindings resolved for the first time?

It is a simple question but I cannot find any information neither in books nor through Google.

like image 652
Pavel Voronin Avatar asked Dec 14 '12 09:12

Pavel Voronin


People also ask

What is data binding used for?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

Should we use data binding?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.

What is data binding with example?

Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

What is data binding and what are some of the benefits it provides?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.


1 Answers

It's not that simple actually, you won't get a straight answer for this question. It depends on the context.

Here are two simple examples :

If the bounded property is owned by a WPF control that is not inside a ControlTemplate, the binding will most likely be resolved for the first time when the UpdateLayout method is called for the first time, if the DataContext is already set. If the DataContext was not set, it will try to be resolved after the control is Loaded: see the DataBindEngine.RequestRun() below

private void RequestRun()
{
    base.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new DispatcherOperationCallback(this.Run), false);
    base.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new DispatcherOperationCallback(this.Run), true);
}

If the bounded property is owned by a WPF control that is inside a ControlTemplate however, it will be resolved for the first time during the first layouting pass that will trigger an ApplyTemplate and lead to resolving the binding.

Those are only specific examples, if you want to fully understand the binding mechanisms, you should use reflector to take a look at MS.Internal.Data.DataBindEngine and System.Windows.Data.BindindExpression classes. Those are the classes responsible for pushing the correct data when using bindings on dependency properties.

like image 200
Sisyphe Avatar answered Sep 30 '22 03:09

Sisyphe