ContentPresenter in ControlTemplate cannot modify attached dependency property

Why doesn't the following simplified code set the TextBlock font size to 50?

<Window.Resources> <ControlTemplate TargetType="ContentControl" x:Key="Test"> <ContentPresenter TextBlock.FontSize="50" /> </ControlTemplate> </Window.Resources> <Grid> <ContentControl Template="{StaticResource Test}"> <TextBlock>Test should be rendered big</TextBlock> </ContentControl> </Grid> 

If I change the value of the FontSize property, visual studio will show me the text the size I want. After compiling or executing the application, the size of the text block always resets to its default size.

I also tested various versions with styles and embedded resources, but I always end up with a situation where I cannot establish the inheritance of the attached dp from within the ControlTemplate containing the ContentPresenter. Is it for design?

+6
wpf controltemplate contentpresenter
source share
3 answers

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.

+12
source share

What about:

 <Window.Resources> <ControlTemplate TargetType="ContentControl" x:Key="Test"> <Border TextBlock.FontSize="50"> <ContentPresenter /> </Border> </ControlTemplate> </Window.Resources> <Grid> <ContentControl Template="{StaticResource Test}"> <TextBlock>Test should be rendered big</TextBlock> </ContentControl> </Grid> 
0
source share

This is interesting because I got something like this to work. Is there any difference?

 <Style x:Key="SingleWaveItemContainerStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Grid Background="{StaticResource WindowBackgroundColor}"> <Border Width="125" x:Name="BorderItem" Height="60" Margin="5" BorderThickness="2" ClipToBounds="True" BorderBrush="{StaticResource ViperPanelBorderColor}" Style="{StaticResource ButtonBorderStyle}"> <Rectangle x:Name="BackgroundRec" Fill="{StaticResource ViperPanelBorderColor}" Stroke="Transparent" Width="125" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> <ContentPresenter Name="TheContentPresenter" Width="115" Height="60" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="BorderItem" Property="BorderBrush" Value="{StaticResource NavBar_HighlightBrush}"/> <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource NavBar_HighlightBrush}"/> <Setter TargetName="TheContentPresenter" Property="TextElement.Foreground" Value="White"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <DataTemplate x:Key="SingleWaveDataTemplate" DataType="ListBoxItem"> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock FontWeight="Bold" Text="{Binding Name, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock FontSize="8" Text="{Binding CreationDate, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/> </StackPanel> </StackPanel> </DataTemplate> 

On the xaml page, I have:

 <ListBox Background="Transparent" ItemTemplate="{StaticResource SingleWaveDataTemplate}" ItemContainerStyle="{StaticResource SingleWaveItemContainerStyle}" BorderThickness="0" ItemsSource="{Binding AllModes, Mode=OneWay}" Height="{Binding ElementName=this, Path=Parent.Height}" SelectedItem="{Binding CurrentSingleWaveModeViewModel, Mode=TwoWay}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Height="{Binding ElementName=Parent, Path=Height}" Background="{StaticResource WindowBackgroundColor}"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> 

Perhaps we need to use data templates to get the desired effect?

0
source share

All Articles