Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Correctly Apply a Style to Content Presenter

Tags:

styles

wpf

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>
like image 343
Clyde Avatar asked Nov 29 '12 14:11

Clyde


1 Answers

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>
like image 147
Arthur Nunes Avatar answered Sep 26 '22 19:09

Arthur Nunes