wpf: usercontrol vs. customcontrol performance issue
Asked Answered
P

5

5

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?

Petulah answered 25/3, 2010 at 17:12 Comment(4)
I would expect neither UserControl nor CustomControl is more performant, but I honestly don't know. What is your control doing? What elements is it composed of? How is it getting its data? These questions may help solve the performance issue.Windjammer
Ed's right. It would also be helpful to know if the control is nested in something else or in a stand alone window. Hopefully you aren't launching wpf windows from a winforms app.Ignoble
Does your tree use a VirtualizingPanel? Doing so may greatly improve performance, since you won't need to load all of your user-controls at once.Windjammer
Another thing to check is how much time you're spending creating the Hierarchical structure vs how long it takes to get that onto the screen. Perhaps reading the Xml and building your objects is where you're spending most of your time. If that's the case then the VirtualizingPanel won't help much.Windjammer
W
2

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

Windjammer answered 25/3, 2010 at 18:5 Comment(3)
@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?Petulah
There's an article here 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.Windjammer
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.Standardize
M
6

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.

Mollie answered 8/7, 2010 at 18:41 Comment(3)
Same experience here. The cost of loading many small UserControls is quite high, a cost which seems to go away once you convert the UserControl to a Custom Control. My guess is that in the latter scenario, expensive stuff like reading Baml and initializing multitudes of Dependancy Properties is done once, instead of once-per-instance.Leftwich
custom control dont load xaml too ?Mccall
Both load XML, but per @Omer Raviv's answer, it appears that XAML (which is converted to BAML) is only read once and cached vs. being read over and over again.Mollie
W
2

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

Windjammer answered 25/3, 2010 at 18:5 Comment(3)
@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?Petulah
There's an article here 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.Windjammer
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.Standardize
I
0

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

Indihar answered 25/3, 2010 at 17:23 Comment(3)
How can I do that? Any examples?Petulah
SuspendLayout doesn't exist in WPF.Picoline
@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?Indihar
S
0

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

http://lucisferre.net/2010/04/21/virtualizing-stack-panel-wpf-part-duex/

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.

Standardize answered 11/5, 2010 at 16:51 Comment(0)
P
0

I worked at Microsoft and was not allowed to use the UserControl because of its poor performance. We always created controls in C#. Not sure about the performance of DataTemplates, but am interested in knowing if it is better. I suspect that it is.

Prostyle answered 15/3, 2016 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.