petko_stankoski
petko_stankoski

Reputation: 10713

Event that gets called when the control finished binding

I have an ItemsControl control. In its items I show a lot of things: images, textblocks, etc.

I have a 'Search' functionality implemented on the itemscontrol - meaning that if the user enters some letters from the keyboard, the items control will be refreshed. My Search method is in code-behind and it takes less than a second. However, the time between I enter the letters and see the results is 3-4 seconds. I have a window closing command and I want to put it in the exact moment before showing the search results. If I put this command in the end of my Search method (in the code-behind), there is still a few seconds delay between closing the window and showing the items. I'm thinking that the binding is slow and that's why i need to catch the event that gets called when the binding finishes. Is there such an event in WPF?

OnPropertyChanged event gets called before the Search methods finishes, so that doesn't help me.

I also tried with the OnDataContextChanged event, but it gets called just once - when the control is initialized. I need it to also get called when the user enters letters and new binding occurs.

Upvotes: 2

Views: 774

Answers (1)

feO2x
feO2x

Reputation: 5728

When you establish a data binding between a source property and a target dependency property in WPF, this Binding is actually translated into a BindingExpression object which does the heavy lifting of updating the source and the target at the appropriate moments.

Unfortunately, BindingExpression does not provide events when something is updated, as you can see here: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpression(v=vs.110).aspx

The only way is to set the UpdateSourceTrigger to Explicit when you define your binding, get the binding expression in code behind and update the source and target manually - then you have full control and can encapsulate your common functionality in this scenario.

You can obtain the BindingExpression by using the BindingOperations.GetBindingExpression static method: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.getbindingexpression(v=vs.110).aspx

If you have any further questions, please feel free to ask.

P.S.: isn't there maybe another way to do this? If you're using a source collection that you bind to your ItemsControl, couldn't you perform the searching / filtering operations on the source collection and just let the collection binding update your ItemsControl?

Upvotes: 3

Related Questions