2015-04-17 36 views
0

我已經有了一個基礎樣式爲什麼我的TextBlock樣式不能從我的基礎樣式繼承?

<Style x:Key="BaseFontControl" TargetType="{x:Type Control}"> 
    <Setter Property="FontSize" Value="13"/> 
</Style> 

和一個TextBlock的風格,我不能使用樣式上面基地風格。它說Target type TextBlock is not convertable to base type Control。我該如何解決這個問題?

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="../XAMLStyles/BaseFontControl.xaml"></ResourceDictionary> 
</ResourceDictionary.MergedDictionaries> 
<Style x:Key="TextStyle" BasedOn="{StaticResource FontControl}" TargetType="{x:Type TextBlock}"> 
    <Setter Property="TextWrapping" Value="Wrap"/> 
    <Setter Property="FontFamily" Value="DroidSans"/> 
    <Setter Property="Foreground" Value="#000000" /> 
</Style> 

回答

1

TextBlock不從Control衍生,因此靶向TextBlock樣式不能被基於一個靶向Control

你可以改變你的基本樣式目標FrameworlElement

<Style x:Key="BaseFontControl" TargetType="FrameworkElement"> 
    <Setter Property="TextBlock.FontSize" Value="13"/> 
</Style> 

這種方法需要從事實TextBlock.FontSizeControl.TextBlock性質使用相同的依賴項屬性的優勢。因此它還設置從Control派生的元素的FontSize屬性。