How Do I Correctly Apply a Style to Content Presenter

15,236

Solution 1

I belive that when you put a Framework element inside a ContentControl, the Template is not applied. If you also declare your TextBlock style as a Button's resource, it works in both cases.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.Resources>
    <Style x:Key="LinkButton"
           BasedOn="{StaticResource ResourceKey={x:Type Button}}"
           TargetType="Button">

        <Setter Property="Width" Value="Auto" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter VerticalAlignment="Center"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}" >
                        <ContentPresenter.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextDecorations" Value="Underline" />
                            </Style>
                        </ContentPresenter.Resources>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
    <StackPanel>
        <Button Style="{StaticResource LinkButton}">Text</Button>
        <Button Style="{StaticResource LinkButton}">
            <Button.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextDecorations" Value="Underline" />
                </Style>
            </Button.Resources>
            <TextBlock Text="Text" />
        </Button>
    </StackPanel>
</Grid>
</Window>

Solution 2

I met with simular trouble yesterday. You can move style setters for text block up, out of Template node, so your styles won't erase after you change content of control.

Share:
15,236

Related videos on Youtube

Clyde
Author by

Clyde

Software Contractor

Updated on October 10, 2022

Comments

  • Clyde
    Clyde over 1 year

    I'm working with this answer to this question about link buttons:

    https://stackoverflow.com/a/3564706/945

    The problem is that the TextDecoration Underline style is only being applied to autogenerated TextBlocks.

    <Button Style="{StaticResource LinkButton}">Text</Button> 
    

    'Text' is underlined

    <Button Style="{StaticResource LinkButton}"><TextBlock Text='Text' /></Button> 
    

    'Text' is not underlined

    Why is it not applying to any TextBlock within the content?

    This is the relevant portion of the style:

    <Style x:Key="LinkButton" 
           TargetType="Button"
           BasedOn="{StaticResource ResourceKey={x:Type Button}}"
           >
    
        <Setter Property="Width" Value="Auto"/>
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter Content="{TemplateBinding Content}" 
                                      ContentTemplate="{TemplateBinding  ContentTemplate}"
                                      VerticalAlignment="Center"
                                      >
                        <ContentPresenter.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextDecorations" Value="Underline" />
                            </Style>
                        </ContentPresenter.Resources>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
  • Clyde
    Clyde over 11 years
    But the whole point is to define a single "LinkButton" style once. Not apply a custom style to every single link button in the application
  • Arthur Nunes
    Arthur Nunes over 11 years
    You can define the complete style in your control resources (or choose the style in the Button's resources as the principal) and define the style in the ContententPresenter as a placeholder based on the principal.