2016-08-23 80 views
0

后,我有一个包含这个<ResourceDictionary>默认样式没有被拾起应用样式关键

<Style TargetType="TextBlock"> 
    <Setter Property="FontFamily" Value="..\..\Fonts\#Roboto"/> 
    <Setter Property="FontSize" Value="14"/> 
    <Setter Property="TextWrapping" Value="Wrap"/> 
</Style> 

这工作得很好。

现在我已经添加了另一种风格:

<Style x:Key="MyText" TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="Red"/> 
    <Setter Property="FontWeight" Value="SemiBold"/> 
</Style> 

然而,改变了我的<TextBlock><TextBlock Style="{StaticResource MyText}"/>后 - 第二届样式块内只有样式回升。例如。现在忽略FontFamilyFontSizeTextWrapping

我怎样才能有TextBlock的默认值,然后添加到它?我不想将“x:Key”添加到“默认”样式,因为这已在整个系统中使用。

回答

2

我认为你只需要在你的键入风格的基础上。见下面的例子。

注意BasedOn="{StaticResource {x:Type TextBlock}}"

<Window.Resources> 
    <Style TargetType="TextBlock"> 
     <Setter Property="FontSize" Value="14"/> 
     <Setter Property="Foreground" Value="Green" /> 
     <Setter Property="TextWrapping" Value="Wrap"/> 
    </Style> 
    <Style x:Key="MyText" TargetType="TextBlock"> 
     <Setter Property="Foreground" Value="Red"/> 
     <Setter Property="FontWeight" Value="SemiBold"/> 
    </Style> 
    <Style x:Key="MyAppendedStyles" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}"> 
     <Setter Property="Foreground" Value="Red"/> 
     <Setter Property="FontWeight" Value="SemiBold"/> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <TextBlock Text="Hello" /> 
    <TextBlock Style="{StaticResource MyText}" Text="Style Key" /> 
    <TextBlock Style="{StaticResource MyAppendedStyles}" Text="Style Key" /> 
</StackPanel>