3
votes

I have a button style like this

<Style x:Key="NormalButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="ContentSite">
                    <Grid x:Name="ContainerGrid">
                        <ContentPresenter x:Name="Presenter" Style="{DynamicResource NormalButtonTextStyle}"/>
...

Where the style for the ContentPresenter is

<Style x:Key="NormalButtonTextStyle" BasedOn="{StaticResource DefaultFontStyle}">
    <Setter Property="TextElement.Foreground" Value="{DynamicResource NormalTextOnDarkBackgroundBrush}" />
...

So then I defined my button like this

<Button Style="{StaticResource NormalButtonStyle}">
    <Button.Content>
        <TextBlock Text="Cancel"/>
...

But the TextBlock does not end up with the NormalButtonTextStyle style. I inspected the elements during runtime and the content presenter definitely has the right text foreground, but then the child TextBlock ends up inheriting from something entirely different and gets a foreground of ControlTextBrush.

Am I misunderstanding how styles are applied to child elements? How am I supposed to define the button so that the style is applied correctly?

3
Have you tried including the ContentPresenter style inside ControlTemplate.Resources?XAMlMAX

3 Answers

3
votes

Add the <Setter /> to the Button style:

<Style x:Key="NormalButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="TextElement.Foreground" Value="{DynamicResource NormalTextOnDarkBackgroundBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="ContentSite">
                    <Grid x:Name="ContainerGrid">
                        <ContentPresenter x:Name="Presenter" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
1
votes

Please have a look at this. "The proper style would be inside ContentPresenter just in this case the ContentPresenter is not part of logical tree."

<Style x:Key="textBlockStyle" TargetType="TextBlock">
    <Setter Property="Background" Value="Blue"/>
</Style>


<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter>
                    <ContentPresenter.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource textBlockStyle}"/>
                    </ContentPresenter.Resources>
                </ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

0
votes

The other answers are probably the right way to fix this, but unfortunately the styles are used in a huge application so I prefer a solution that only affects the button. The question linked to by Parisa helped; I need to set the Content of the button directly so that the text's parent is the ContentPresenter:

<Button Content="Cancel" Style="{StaticResource NormalButtonStyle}"/>