Search code examples
wpfxamlresourcestextblock

How to make an exception for Application.Resources style in a specific Grid.Resources


I am doing small WPF app for my own using Visual Studio, C#, .NET Standard and WPF in this specific project.

I have defined style for all TextBlocks and TextBoxes in Applications.Resources like below.

<Application.Resources>
    <Style TargetType="TextBox">
        <Setter Property="FontSize" Value="10"/>
    </Style>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="10"/>
    </Style>
</Application.Resources>

Then in main window I have a grid which contains some buttons.

<Grid>
    <Grid.Resources>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="50" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="50"/>
        </Style>
    </Grid.Resources>
    <Button Grid.Column="0" Content="DASHBOARD" Command="local:CustomCommands.ShowDashboard"/>
</Grid>

I would like to set for the textblocks/textboxes in this specific buttons a wider font.

I tried for many different syntax but could not manage it. I tried also do define x:Key for this style in Grid.Resources and use it in this specific Button control. This wasn't work either.

Can anyone let me know which way should I let know my application that text in this buttons would have bigger font size?


Solution

  • The TextBlock created for string contents by the ContentPresenter inside the Button template doesn't apply the locally-defined resources, i.e. those in your Grid.

    The easiest way to solve your problem would be to explicitly define a TextBlock as the Button's content.

    <Grid>
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="50"/>
            </Style>
        </Grid.Resources>
        <Button Grid.Column="0" Command="local:CustomCommands.ShowDashboard">
            <TextBlock Text="DASHBOARD" />
        </Button>
    </Grid>