2010-06-17 56 views
6

为什么以下简化代码未将TextBlock的字体大小设置为50?ControlTemplate中的ContentPresenter无法更改附加依赖项属性

<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> 

如果我更改FontSize属性的值,Visual Studio会显示我想要的大小的文本。编译或执行应用程序后,文本块的大小始终会重置为其默认大小。

我也测试了不同版本的样式和嵌入式资源,但我始终处于无法在包含ContentPresenter的ControlTemplate中设置继承附加dp的情况。这是设计吗?

+0

以前从未有过这样的情况,但可以通过设计。我认为ContentPresenter只是将您提供给它的内容替换为自己。 – decyclone 2010-06-17 15:51:34

回答

12

我发现这种现象的原因 - 它的设计:

如果ContentControl中的内容已经是一个WPF的元素,它是在使用它之前创建ContenPresenter。元素的逻辑因此ContentControl。我可以通过改变ContentControl中的标记为以下检查:

<ContentControl Template="{StaticResource Test}" TextBlock.FontSize="50">     
    <TextBlock> 
      This text now is shown with a size of 50 
    </TextBlock>      
</ContentControl> 

在这个例子中文字尺寸50如所期望。我可以用Visual Studio的wpf-visualizer证明这个论点。父级是ContentControl,通过dp继承,FontSize从父级(ContentControl)中获取,文本显示为50!

另一个行为可如果ContentControl中只包含文本内容可以观察到:

<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> 

在这种情况下,文本框通过ContentPresenter创建,因为文本无法在视觉输入树。该文本框没有父项,但TemplateParent属性导致ContentPresenter作为TextBoxes父项,DP系统通过从ContentPresenter附加的依赖项属性继承获取FontSize值。这就是为什么在这种情况下字体大小更改为50.

不同的场景描述为here

我不明白的是,为什么VS2010在编译之前显示FontSize 50。

0

如何:

<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

同样的问题在这里... – HCL 2010-06-17 15:51:26

+0

我看到它正常工作后,直接从我的代码编辑器复制它! – decyclone 2010-06-17 19:01:23

+0

你编译并运行它吗?当我在编辑器中复制代码时,它可以工作。编译后失败。我在.net 3.51下测试了它,现在也是.net 4,同样的问题。 – HCL 2010-06-17 19:22:56

0

这很有趣,因为我得到了一些东西就像这样工作。有区别吗?

<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> 

在XAML页面我有:

<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> 

也许我们不得不使用数据模板,以获得预期的效果?

+0

Doh,这很明显,为什么这是工作。文本块由数据模板放置。这意味着对控制模板的任何影响都会导致这种情况发挥作用。 – 2010-08-12 21:00:04