I was answering another question on here where the user had a ListView with an ItemsSource containing UserControls. I said I wouldn’t recommend it, and got asked why.
This really surprised me. I’ve never considered it before. I know it’s not a good idea to do so, but I never really thought about why it wasn’t a good idea to do so.
The only thing I can think of is that you are creating UIElements in memory for every item in your collection, which can be much heavier than data objects. This not only increases the memory your application uses, but also prevents you from using Virtualization. And it doesn’t fit in with the MVVM design pattern, which I use almost religiously when working with WPF.
So, can someone list me all the reasons you should not be using a list of UserControls as an ItemsSource? Or if you think otherwise, why you would?
Basically I want something to point people to when they ask me why they shouldn’t use List<MyUserControl> and ItemsSource="{Binding MyUserControlList}" in their applications.
Your points about performance overhead are very good.
I would ask the converse question….why WOULD you want to?
I’ve seen this practice in VB6 in the past. The developer stores information in user controls in an array somewhere and uses it to access information outside the lifetime on the UI which initially displays that control.
This pattern violates the separation of business logic, model, and user interface.
There’s a fine line between being lazy and being sloppy….reuse and misuse. I’m all about code reuse…but when a developer tells me they want to use user controls to carry information between different areas of the software, I think that falls on the side of misuse. It adversely affects maintainability.
So, if the answer to “why would you want to?” has something to do with using user controls to pass around information, the above would certainly apply.
P.S.
It’s unclear to me what the intent was in the question you linked to. Also, there are valid reasons to binding to other UI elements in the same context (usually using relative binding sources).