2016-10-12 17 views
0

我有以下的复选框默认的文本块造型

<CheckBox x:Name="checkBox" Content="CheckBox" Width="74"/>

和我有一个按钮

<Button Name="TestButton" Content="Test" />

我想设置为文本块“默认”的色彩。我做到这一点通过具有具有下列内容资源字典:

<Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground" Value="White"/> 
</Style> 

的问题是,该按钮还是应该有一个黑色的文字块的前景,但但在另一sourcedictionary我有以下,但仍然变为白色:

<Style TargetType="Button"> 
    <Style.Setters> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border BorderThickness="1,0,0,1" CornerRadius="5" Background="{TemplateBinding Background}"> 
         <ContentPresenter 
           x:Name="ContentPresenter" 
           Margin="1" 
           VerticalAlignment="Center" 
           HorizontalAlignment="Center" 
           TextBlock.Foreground="Black" 
           Opacity="1.0"/> 
        </Border> 
       </ControlTemplate/> 
      <Setter.Value/> 
     </Setter> 
    </Style.Setters> 
</Style 

编辑: 的ResourceDictionaries在Application.xaml定义是这样的:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="TextBlock.xaml"/> 
      <ResourceDictionary Source="Buttons.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
+0

在ContentPresenter中,您是否尝试过'TextElement.Foreground =“Black”'而不是'TextBlock.Foreground'? –

+0

是的,'TextElement.Foreground'和'TextBlock.Foreground'都改变了Text的颜色,但是仍然被其他风格所覆盖。 – Splinti

+0

如何定义在'ContentPresenter.Resources'其他默认TextBlock的风格? –

回答

1

你可以尝试通过定义在其Resources另一个本地覆盖默认TextBlock样式为ContentPresenter

<ContentPresenter ... > 
    <ContentPresenter.Resources> 
     <Style TargetType="TextBlock"> 
      <Setter Property="Foreground" Value="Black" /> 
     </Style> 
    </ContentPresenter.Resources> 
</ContentPresenter> 

但更好的方法来设置默认控件文本颜色是这样的App.ResourcesTextElement.Foreground将在任何给定的单个元素上覆盖此值。

<SolidColorBrush 
    x:Key="{x:Static SystemColors.ControlTextBrushKey}" 
    Color="White" 
    /> 

如果使用和丢弃您的默认TextBlock风格,你原来的你了吧ContentPresenter应该工作。