wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild

Recently I was trying to debug into one mysterious issue where the DockManager from DevExpress is so slow when certain theme is applied.

And I have noticed that there are hotspot such as AddLogicalTree and RemoveLogicalChild on the performance counter. 

Below is a analysis on the result of analysis.


When I drap out a window from the dock manager and put that into a floating group, what I have observed is that first 

1. RemoveLogicalChild is called on its existing logical parent (I blieve that the DockingManager must have certains groups such as FloatingGroup and DockingGroup so that each item/tab falls part of ..)

wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
 

Where then the detached item will be added into a new container, and the AddLogicalChild is called. 


wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
 

and then this Item will be boxed into a new group, which you will see again the AddLogicalChild called again.


wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
 

Also, as I noticed that when a Item detached from the DockingManager and become part of the floating window, you will see the WndProc will be invoked.



wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
 

so the analysis of the result is that extensively uses of the styling through template. Where the logicalTree adding/removing will trigger a huge amount of recalculationg - creating those VisualElement and others will severely bog down the performance.  - As that Styling through the Template can be confirmed though this MSDN page - FrameworkElement.AddLogicalChild. method. 

And this has posed a very interesting question, how do you get the styling done whiles keep the performance.