2010-06-17 54 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