wpfoptimizationlogical-tree

WPF optimization: logical trees in xaml, how are they being built?


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?


Solution

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