Prevent ApplyTemplate () call during measurement phase

I am currently creating a virtualized TreeView control for an application I'm working on. My current implementation is based on an optimized measurement algorithm that gives me some problems. Now, before I find out the details, please note that I am using .NET version 4.0.

What I did to optimize the measurement of the internal TreeView element minimizes the work done in the MeasureOverride () function. I got to the simplest things and now I remain a costly challenge in the back of WPF. Basically, calling Measure () on a control applies all the templates on auxiliary controls, which is very expensive. Since my TreeViewItems will have a certain height, I do not need a template at the measurement stage. Does anyone know a workaround for calling ApplyTemplates () ?

Here's an example call stack

  • MyCustomControl.MeasureOverride (...)
    • ...
      • UIElement.Measure (...)
        • FrameworkElement.MeasureCore (...)
          • FrameworkElement.ApplyTemplate (...) <- Expensive!
+4
source share
2 answers

I'm not sure that you can avoid calling ApplyTemplate() in your specific case, but you don't need to. WPF TreeView has built-in support for user interface virtualization:

<TreeView VirtualizingStackPanel.IsVirtualizing="True" />

Learn more about this page .

0
source

If you have not solved this problem yet, I have an idea that I have not tried: maybe you can write your own panel to contain the elements of the tree, where you would redefine MeasureOverride so that it would not get the measurement from the contained elements, but rather do this calculation yourself?

-one
source

Source: https://habr.com/ru/post/1502914/


All Articles