-1

I have a default Label Style

<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="13.333" />
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>

<Style BasedOn="{StaticResource LabelStyle}" TargetType="{x:Type Label}" />

I then try to use a different style for a single Label

    <Style x:Key="HeaderLabelStyle" TargetType="{x:Type Label}">
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Foreground" Value="{StaticResource HeaderForegroundBrush}" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    </Style>

<Label  Content="Text here"  Name="someName" Style="{StaticResource HeaderLabelStyle}"/>

But for some reason the label always gets the default style. Why? Can this be overridden?

Thanks

JAnton
  • 1,095
  • 8
  • 16
  • 1
    Where are your styles located? `Application.Resources`, `Window.Resources`, etc? It could be [this](https://stackoverflow.com/q/9035878/302677) is the issue. – Rachel Sep 05 '17 at 14:59
  • Yeah, unless you want an implicit style to apply globally to your entire application, I'd recommend placing it in `Window.Resources` instead. Everything in `Application.Resources` should either have an `x:Key`, or be something that is actually global. – Rachel Sep 05 '17 at 15:15
  • Glad you got it sorted out :) But it should be noted that `Label` and `TextBlock ` inherit from different base controls, and use different rules. A `Label` inherits from `Control` which has rules such as respecting template boundaries. `TextBlock` inherits from `FrameworkElement`, which does not. – Rachel Sep 05 '17 at 15:44
  • 1
    Label is rendered using the FrameWorkElement of TextBlock to render the text, which is why you see the TextBlock styles applied. It doesn't inherit anything from TextBlock directly. See [here](https://msdn.microsoft.com/en-us/library/dd894496(v=vs.95).aspx) for how the Label is default templated. It is a Border+ContentPresenter, and ContentPresenter default draws text content using the TextBlock framework element. – Rachel Sep 05 '17 at 16:09

1 Answers1

0

So I realised that the default (string) template for Label is an indented TextBlock (styles are inherited)

Since I also was defining a global style for TextBlock

<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" />
<Setter Property="FontSize" Value="13.333" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Style BasedOn="{StaticResource TextBlockStyle}" TargetType="{x:Type TextBlock}" />

No matter how many types of Label I had I was always bound to use the template for TextBlock

So the solution was to define a dummy TextBlock class

namespace Theme
{
    public class HeaderTextBlock : TextBlock
    {
    }
}

Then assign it its own global style

xmlns:this="clr-namespace:Theme"
 <Style x:Key="HeaderTextBlockStyle" TargetType="this:HeaderTextBlock">
    <Setter Property="Foreground" Value="{StaticResource HeaderForegroundBrush}" />
    <Setter Property="FontSize" Value="13.333" />
    <Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style BasedOn="{StaticResource HeaderTextBlockStyle}" TargetType="{x:Type this:HeaderTextBlock}" />

And use TextBlocks instead of Labels (haven't figured out how to implement a Label child yet since (Label : TextBlock) = false

xmlns:theme="clr-namespace:Theme;assembly=Theme"
<theme:HeaderTextBlock Text="Some text"  Name="titleLabel" Style="{StaticResource HeaderTextBlockStyle}"/>
JAnton
  • 1,095
  • 8
  • 16
  • 3
    _"Label is a glorified TextBlock"_ -- not at all. `Label` is a `ContentControl`, which means it is capable of presenting a wide range of content, based on templates available. Yes, if your content is a `string`, the default template is a `TextBlock`. But `Label` is _far_ more than just _"a glorified TextBlock"_. – Peter Duniho Sep 05 '17 at 17:03