I have created a custom style for Labels in my application, this works fine and I apply a Template to achieve the other set styles that I need, again this works fine.
STYLE DEFINITIONS:
<Style TargetType="{x:Type Label}">
<Setter Property="Background" Value="{DynamicResource BackgroundBrush}" />
<Setter Property="Cursor" Value="Arrow" />
<Setter Property="FontSize" Value="{DynamicResource Font_Regular}" />
<Setter Property="FontWeight" Value="{DynamicResource 400}" />
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Template" Value="{DynamicResource Label.Default}" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<ControlTemplate x:Key="Label.Default" TargetType="{x:Type Label}">
<TextBlock Style="{DynamicResource Label.TextBlock}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Text="{TemplateBinding Content}"
VerticalAlignment="{TemplateBinding VerticalAlignment}" />
</ControlTemplate>
<ControlTemplate x:Key="Label.ItemHeading.Emphasis.Highlight" TargetType="{x:Type Label}">
<TextBlock x:Name="lblContent"
FontWeight="{DynamicResource 500}"
Foreground="{DynamicResource HighlightBrush}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Style="{DynamicResource Label.TextBlock}"
Text="{TemplateBinding Content}" />
</ControlTemplate>
- - -
<Style x:Key="Label.TextBlock" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontFamily" Value="{DynamicResource Inter}" />
<Setter Property="FontSize" Value="{DynamicResource Font_Regular}" />
<Setter Property="FontWeight" Value="{DynamicResource 400}" />
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
<Setter Property="Padding" Value="5" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
USAGE:
<Label Content="{Binding Reseller}"
ContentStringFormat="Powered by {0}"
Grid.Column="1"
HorizontalAlignment="Center"
Template="{DynamicResource Label.ItemHeading.Emphasis.Highlight}"
VerticalAlignment="Center"
x:Name="lblPoweredBy" />
I have now hit one short coming of this control and it relates to my Label having a ContentStringFormat value:
EXPECTED:
Label (lblPoweredBy) should output “Powered by MyCompany”, but instead it just outputs “MyCompany”
I tried adding StringFormat {TemplateBinding ContentStringFormat} to the Text property of the TextBlock, but at runtime it says this is incompatible. Can anyone help out?