0

I've been profiling my application and finding that a lot of the delays are due to the WPF initializations. I've found an article on WPF optimization saying that building a logical tree top-down will have better performance than if it were built bottom-up. The example in the article is in C#. I'm wondering, when the UI is done in XAML, how is it building the tree?

Dan Vogel
  • 3,858
  • 7
  • 41
  • 57

1 Answers1

3

When it is done in XAML, it is done top down.

This article refers to building a tree from code. You want to add top level elements, then children, then their children and so on. If you add the children first, then parent elements, then their parent elements, and so on, you will incur a major performance cost due to invalidation of all children in the tree instead of just the path back to the root through each parent.

Build Your Tree Top-Down

When a node is added or removed from the logical tree, property invalidations are raised on the node's parent and all its children. As a result, a top-down construction pattern should always be followed to avoid the cost of unnecessary invalidations on nodes that have already been validated.

Jerry Bullard
  • 6,116
  • 18
  • 27
  • I'm wondering, say I add a Grid, and then its children, wouldn't the Grid get invalidated due to its children and have to go through the layout pass again? And then, for each children added inside, could potentially invalidate the parent again, walking all the way up the tree. Wouldn't this actually have been better if it was built bottom up? –  Feb 25 '15 at 02:21