Sdílet prostřednictvím


WPF Perf, Element Count, Panels and Controls

I got a question about WPF performance this morning regarding how the number of elements (controls, panels, et al.) affects performance.  Before I share more broadly, I'll mention that the WPF Performance team is working on plans for more detailed & prescriptive guidance; you should stay tuned here & MSDN for more info.  In the mean time, here's the response I provided:

--------

There is a cost for additional elements that you add; to name a few:

  1. Working set – you’ve got more elements and more state that consumes memory.

  2. Layout – you’re performing more layout work even if these are fairly simple calls.

  3. Property invalidations – when a property changes there’s more work to update affected values.

  4. Hit testing – more elements to check

    when input is received.

One of the key ways to improve performance of your app is to reduce the number of elements. You can do this in a variety of ways:

  • Use virtualization when you can in ItemsControls using the VirtualizingStackPanel.
  • Use the Grid element as a top‑most layout when you have a fairly complex & nested layout instead of several panels.
  • Reduce the number of elements in and depth of templates when you can.

Comments