1
votes

I defined a style for Label in App.xaml.

 <Application.Resources>
    <ResourceDictionary>
        <Style TargetType="Label" >
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
        <Style TargetType="TextBlock" >
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>

The Style applied to the label Control in my MainWindow.xaml proerly. but when i tried to have a Foreground set explicitly on the control, it was not working (I wonder). The color defined in App.xaml is still applying (only for Label).

<Grid>
    <Label Content="Label" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <TextBlock Text="TextBlock" Foreground="Black" VerticalAlignment="Bottom" Height="15.96" Margin="257.537,0,270.003,86" />
</Grid>

The same logic works for Textblock and all controls. Any suggestions please?

2

2 Answers

6
votes

Your Label's Foreground will be displayed in blue because of style set for the TextBlock

<Style TargetType="TextBlock" >
    <Setter Property="Foreground" Value="Blue"/>
</Style>

You can check more details about this in

Differences between Label and TextBlock

4
votes

That is an explaination, but not a solution ;)

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontFamily"
            Value="Tahoma" />
    <Setter Property="FontSize"
            Value="{StaticResource StandardFontSize}" />
    <Setter Property="Foreground"
            Value="Black" />
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="HorizontalAlignment"
            Value="Left" />
</Style>    
<Style TargetType="{x:Type Label}">
    <Setter Property="Foreground"
            Value="Black" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}"
                           Foreground="{Binding RelativeSource={RelativeSource AncestorType=Label}, Path=Foreground}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>            
</Style>

If you define you styles like this then you are able to also set/override your Label Foreground in your grid. So these styels default labels and textblocks to Black but you can change to blue if you like.