How to create an instance of ControlTemplate in XAML?

The a code ControlTemplatecontains a hierarchy FrameworkElementFactorythat can be used to build the elements of the framework themselves. But in XAML, the content is ControlTemplatedisplayed as the elements themselves.

Does the XAML parser transform from one form of an object to another, is it used ValueConverteror actually creates factories when it parses the content ControlTemplate?

+3
source share
1 answer

This is done differently if it is defined in XAML and in code. To check, you can create a simple project in WPF with one window and add a custom control that has a ControlTemplate defined in XAML by default. Then add the control to your window and add a button whose Click handler contains a breakpoint.

When a breakpoint hits, we can check the user control and the ControlTemplate. The VisualTree property (which is FrameworkElementFactory) is null.

Reflector, StyleHelper.ApplyTemplateContent. if FrameworkElementFactory ( VisualTreeProperty). if-statement ControlTemplate XAML, FrameworkTEmplate.LoadOptimizedTemplateContent.

+3

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


All Articles