I found the reason for this behavior - its design:
If the Content ContentControl is already a WPF element, it is created before using it in ContenPresenter . The logical parent of the element is therefore ContentControl . I can verify this by changing the layout of the ContentControl to the following:
<ContentControl Template="{StaticResource Test}" TextBlock.FontSize="50"> <TextBlock> This text now is shown with a size of 50 </TextBlock> </ContentControl>
In this example, the text size is 50. I can prove this argument with the wpf visualizer of the visual studio. The parent is a ContentControl and through dp-inheritance, FontSize is taken from the parent (ContentControl), and the text is displayed with a size of 50!
Other behavior can be observed if ContentControl contains only text as content:
<Window.Resources> <ControlTemplate x:Key="Test" TargetType="{x:Type ContentControl}"> <ContentPresenter TextBlock.FontSize="50"/> </ControlTemplate> </Window.Resources> <Grid> <ContentControl Template="{StaticResource Test}"> This text is shown with a size of 50 </ContentControl> </Grid>
In this case, a TextBox is created through ContentPresenter , because the text cannot be entered into the visual tree. The text field does not have a parent, but the TemplateParent property causes the ContentPresenter to be the parent of the TextBoxes, and the DP system takes the FontSize value through the attached dependency inheritance property from ContentPresenter. Therefore, in this case, the font size changes to 50.
Various scenarios are described here .
I do not understand why VS2010 shows FontSize 50 before compilation.
HCL
source share