开发者

How does one gauge/know the weight of an element?

开发者 https://www.devze.com 2022-12-22 06:06 出处:网络
I\'m working on the UI side of a WPF project. My favourite reference while XAML\'ing at the moment is Adam Nathan\'s \"Windows Presentation Foundation Unleashed\". He gives the following tip regarding

I'm working on the UI side of a WPF project. My favourite reference while XAML'ing 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 t开发者_如何学Gootally understand.

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?


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号