Jack Straw
Jack Straw

Reputation: 293

WPF - Use control inheritance for standard-styled controls?

I have a WPF application with many windows and user controls, and I'd like to implement standard styles for certain controls that appear throughout the application. As an example, say I need two standard TextBlocks throughout the application: one for large headings, one for small headings. And the only difference between them is the font size, say 36 and 24 respectively. All other properties (color, fontfamily, etc.) could be set by a TextBlock template or global TargetType="{x:Type TextBlock}" styles.

Of course I could create two global named styles that just set the font size and apply those staticresource styles liberally throughout the XAML to my TextBlocks, or at the highest possible level above the TextBlocks that would not interfere with other TextBlocks. But as an alternative, which would remove the requirement for setting the Style tag in many places, is inheriting from TextBlock is a good way to go?

TextBlock controls:

class TextBlockLargeHeading : TextBlock
{
    public TextBlockLargeHeading()
    { }
}

class TextBlockSmallHeading : TextBlock
{
    public TextBlockSmallHeading()
    { }
}

Global resource:

<Application.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red" />
    </Style>
    <Style TargetType="MyApp:TextBlockLargeHeading" BasedOn="{StaticResource {x:Type TextBlock}}" >
        <Setter Property="FontSize" Value="36" />
    </Style>
    <Style TargetType="MyApp:TextBlockSmallHeading" BasedOn="{StaticResource {x:Type TextBlock}}" >
        <Setter Property="FontSize" Value="24" />
    </Style>
</Application.Resources>

Then, to use them anywhere, simply reference the custom textblocks:

<StackPanel>
    <MyApp:TextBlockLargeHeading Text="Large" />
    <MyApp:TextBlockSmallHeading Text="Small" />
</StackPanel>

Which would create two Red TextBlocks with the appropriate font sizes.

Is this a reasonable approach? Are there any gotcha's if I've got 100's of instances of these, maintainability-wise or otherwise? Is there a better (safer or less code/XAML) approach? Perhaps using User Controls instead?

Thanks!

Upvotes: 0

Views: 330

Answers (1)

Ritch Melton
Ritch Melton

Reputation: 11618

There's no reason to do all that. Create your styles and use them directly.

....
<Style x:Key="DefaultTextBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="Red" />
    <Setter Property="FontSize" Value="24" />
</Style>
<Style x:Key="LargeTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource DefaultTextBlockStyle}">
    <Setter Property="FontSize" Value="36" />
</Style>

<!-- Style applies to all TextBoxes -->
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DefaultTextBlockStyle}" />
...

<StackPanel>
    <TextBlock Text="Large" Style="{StaticResource LargeTextBlockStyle}"/>
    <TextBlock Text="Small"/>
</StackPanel>

Upvotes: 1

Related Questions