[英]WPF SubControl (like TextBlock) doesn't inherit Style from window with TemplateSelector
[英]Why doesn't my TextBlock style inherit from my base style?
我有一個基本的風格
<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>
TextBlock
不是從Control
派生的,因此,針對TextBlock
的樣式不能基於針對Control
的樣式。
您可以將基本樣式更改為目標FrameworlElement
:
<Style x:Key="BaseFontControl" TargetType="FrameworkElement">
<Setter Property="TextBlock.FontSize" Value="13"/>
</Style>
該方法利用了TextBlock.FontSize
和Control.TextBlock
屬性使用相同的依賴項屬性這一事實的優勢。 因此,它還會設置從Control
派生的元素的FontSize
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.