views:

38

answers:

1

I'm working on the UI side of a WPF project. My favourite reference while xaml'ing (besides stackoverflow :) ) at the moment is Adam Nathan's "Windows Presentation Foundation Unleashed". He gives the following tip regarding control templates: "Rather than using a ContentControl inside a control template, you should use the lighter-weight ContentPresenter element".

The developers I work with will often also tell me to use the most lightweight elements I possibly can in all of the xaml'ing I do - which I totally understand.

My (naive!) question is this: how do I find out what the relative "weights" of different elements are? There are obvious things, like a RichTextBox versus a TextBox. But how do I figure out, for instance, whether a StackPanel is better than a Grid in a situation where either can do the job?

Is an element/control's visual tree a good indicator of "weight"? What else can I look at?

Thanks in advance.

+1  A: 

When it comes to things like ContentPresenter vs. ContentControl, you can check the inheritance hierarchy: ContentPresenter derives from FrameworkElement whereas ContentControl derives from Control.

As far as panels go, basically Grid has the most complex layout logic (which is why it is the most flexible), so generally any other panel is going to be better if it can get the job done. If you want a concrete guide to panel complexity, I would list them in the following order from least to most complex:

  1. UniformGrid - The child size isn't even used to calculate the panel's extent
  2. StackPanel - Only one dimension of the child's size is used
  3. WrapPanel - Similar to StackPanel, but a little more complex as the widest element of a column/row have to be remembered.
  4. DockPanel - Since only one element can take up all remaining space, I'd say it is lighter weight than Grid.
  5. Grid - The most flexible, and therefore has the most complex layout algorithm.
Abe Heidebrecht
Hi AbeFirstly, thanks.From your answer: "...you can check the inheritance hierarchy: ContentPresenter derives from FrameworkElement whereas ContentControl derives from Control."So something that derives from Control will always be "heavier" than something that derives from say, FrameworkElement.Panel, because Controls have control templates that include a load of functionality?And: "Grid - The most flexible, and therefore has the most complex layout algorithm."i.e. flexibility is usually a good indication of weight, too.(I'm thinking out loud here, so correct me if I'm wrong!)
cfouche
Yeah, I agree with all of your clarifications. I would say that Controls' being templated is really what makes them heavier. Since you can redefine the visual tree of a control as a global style, as a control author, you would have no idea how many visuals you would be adding...
Abe Heidebrecht