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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With