Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm still new to WPF (coming from Winforms). I'm busy with an WPF application that will listen for and receive strings via TCPIP and display them in listviews on various tabs in the window.
I plan to fill a few ObservableCollections as I receive these strings. Then have those databound to listviews on a few tabs of a tabcontrol.

My question is, how is performance affected with databinding with controls that are not shown. Should i be paying attention to which tab the user is currently viewing and only populating that particular observablecollection? Or does WPF know which listviews are not currently visible and only do databinding as it's needed.

Also, using this method I am expecting to run into CrossThreadExceptions (or whatever the WPF equivalent is), so any advice for that would help as well.

What I have tried:

Havent tried anything yet. Looking for advice.
Posted
Updated 29-Nov-17 20:09pm

If you are using databinding with the ObservableCollection, then the answer is yes.

To confirm, try it. Create a new project, add a listbox, set the Items datasource, add two buttons, one to toggle visibility state, the other to Clear (not new) and reload the ObservableCollection with dummy data. Run, click hide button, click reload button, click hide button again to show. You will have your confirmed answer.
 
Share this answer
 
I think I see where you are going with this question, but binding data in of itself is a very low cost operation. The particular part of binding collections to controls that can cause performance impact is the generation/rendering of the UI elements to display those items.

The latter part is mitigated in WPF via virtualization. So let's say your ListBox is hidden, or maybe has just 10 rows visible, and you bind a 10,000 element list to it. How much will that impact performance? Without going into extensive detail on virtualization, that answer is not much at all for normal UI display.

Now there's one more thing in this equation that can impact performance, and that's retrieving that initial list of 10,000 elements right? Perhaps it's a complex database call, or working with a hefty API... Hopefully this list is lazy loaded, but even when you do that if are binding data to a ListBox that binding will still occur and try to initialize that object even if it's hidden - and that may be a useless expense.

In this latest scenario you still want to use data binding; however, you want to initialize/populate the elements when the control is actually in view (i.e. on the current TabItem in a TabControl). There are numerous ways to do that, but generally your view will call an initialize method/command on your view model. If the view is already bound adding elements to the existing collection may cause cross-threading issues; however, if you set the property to a new collection you won't. And if you're working with large collections typically this approaches performs best so you're not firing notifications with each addition.

That all being said, both approaches have their pros and cons. If you're going to do background work and want to avoid threading issues I'd take a look at this article:

Multithreading in C# .NET 4.5 (Part 2)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900