When are data bindings applied?

后端 未结 1 378
南笙
南笙 2021-01-02 09:41

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 Goo

相关标签:
1条回答
  • 2021-01-02 10:05

    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.

    0 讨论(0)
提交回复
热议问题