views:

233

answers:

4

Which one is better from performance view user control or custom control? Right now I am using user control and In a specific scenario, I am creating around 200(approx.) different instances of this control but it is bit slow while loading and I need to wait atlest 20-30 second to complete the operation. What should I do to increase the performance?

Edit:

The scenario is: In my Window, I have a TreeView, each item of this represents different user-defined types, So I have defined DataTemplate for each type. These DataTemplates are using user-controls and these usercontrols are binded with properties of user-defined types. As simple, TreeView maps a Hierarchical Data Structure of user-defined types. Now I read from Xml and create the Heirarchical structure and assign it to TreeView and it takes a lot of time to load. Any help?

A: 

Make sure to SuspendLayout while adding controls en masse. Try to completely configure the control before adding it to any container.

Ben Voigt
How can I do that? Any examples?
viky
SuspendLayout doesn't exist in WPF.
Maurizio Reginelli
@Maurizio: oops. TreeView and UserControl are both winforms classes, which threw me off. But not having SuspendLayout is pretty bad, since adding a control is one of the things that triggers relayout in WPF. And there's no AddRange method on UIControlCollection either, to avoid repeated relayout. Perhaps adding the children to an off-screen control first, then adding the parent to the Window, might cause a single Layout operation?
Ben Voigt
+2  A: 

FYI: Here's a link on using a VirtualizingPanel with the TreeView: http://msdn.microsoft.com/en-us/library/cc716882.aspx

Ed Gonzalez
@Ed Yeah, you are right! But in my case I am avoiding to use that(I have set it to false), since i need to select some TreeViewItem through code and bring that into focus. Anyway thanks for help, Is there any other way?
viky
There's an article here http://bit.ly/cZQdGy where someone is trying to navigate while using a virtualized TreeView. A couple of tentative solutions are linked there, maybe one can be molded to help.
Ed Gonzalez
That's my blog post and I did end up finding a reasonable decent way to navigate while virtualizing, however it still felt very "hacky". I will be posting it soon. In the end I opted for a different solution where instead of navigating to items I allowed to user to filter to a specific item by changing the CollectionView filter property (bound to a property in the viewmodel). I will be posting that approach as well. Sorry I don't have time right now, but if you just want the code I can try to gist it for you.
Chris Nicola
A: 

Here is the follow-up article to my issues with WPFs Virtualizing Stack Panel and TreeView. I hope this helps you.

http://www.lucisferre.net/post/2010/04/21/Virtualizing-Stack-Panel-WPF!-Part-Duex.aspx

Long story short: It is possible to do the navigation with the current VSP, but it is a bit of a hack. The current VSP design needs a rework, as the way it currently virtualizes the View breaks the coupling between the View and ViewModel which, in turn, breaks the whole concept of MVVM.

Chris Nicola
A: 

I have an application that is loading around 500 hundred small controls. We originally built these as user controls, but loading the baml seems to cause the controls to load slow (each one is really fast, but when we get around 300, the total of all of them together seems to add up). The user controls also seem to use up a good amount of memory. We switched these to custom controls and the app launches almost twice as fast and takes up about 1/3 the ram. Not saying this will always be the case, but custom controls made a big difference for us.

Michael Baer