簡體   English   中英

為什么我的TextBlock樣式不繼承自基本樣式?

[英]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.FontSizeControl.TextBlock屬性使用相同的依賴項屬性這一事實的優勢。 因此,它還會設置從Control派生的元素的FontSize屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM