asusralis
Topic Author
Posts: 60
Joined: 30 Jul 2018, 05:03

Unable to change button font size

18 Sep 2019, 07:52

What could cause not being able to change a button's font size? I have a button style defined in my Resources, and all properties set in it change every button except the font size. I have a TextBlock style that sets its FontSize to a static resource, but removing that does not help things. I can't find any other button style it could be using, so I'm confused as to why buttons refuse to change their text size.
 
User avatar
sfernandez
Site Admin
Posts: 1785
Joined: 22 Dec 2011, 19:20

Re: Unable to change button font size

18 Sep 2019, 13:19

I tried the following xaml in our XamlPlayer to test different ways to define the FontSize and Button content and all behave as expected:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <Style x:Key="btnStyle" TargetType="Button">
            <Setter Property="FontSize" Value="20"/>
        </Style>
        <ControlTemplate x:Key="btnTemp" TargetType="Button">
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Grid.Resources>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Button" FontSize="20"/>
        <Button FontSize="20">
            <TextBlock Text="Button"/>
        </Button>
        <Button Content="Button" Style="{StaticResource btnStyle}"/>
        <Button Content="Button" Template="{StaticResource btnTemp}" FontSize="20"/>
    </StackPanel>
</Grid>
What could you have different in your styles+xaml?
 
asusralis
Topic Author
Posts: 60
Joined: 30 Jul 2018, 05:03

Re: Unable to change button font size

18 Sep 2019, 22:49

These are the only relevant styles in my resources:
    <Style x:Name="TextBlockStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource FontColor}" />
        <Setter Property="FontFamily" Value="{StaticResource Font}" />
        <Setter Property="FontSize" Value="{StaticResource FontSize.Medium}" />
    </Style>

    <Style TargetType="Button">
        <Setter Property="FontSize" Value="{StaticResource FontSize.Medium}" />
        <Setter Property="Background" Value="Pink"/>
    </Style>
The background of the button changes, but the font size does not. However, the background does turn pink. If I understand correctly, buttons use a TextBlock which means I wouldn't have to set the font size in the button style? Either way, setting the font size on a specific button also does not change its font size.
                    <Button
                        Content="Text"
                        FontSize="72" />
I've searched my entire project but there are no other button or text block styles.
 
User avatar
sfernandez
Site Admin
Posts: 1785
Joined: 22 Dec 2011, 19:20

Re: Unable to change button font size

18 Sep 2019, 23:26

I tested the following xaml in Unity and the Button changes its size correctly:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid.Resources>
        <sys:Double x:Key="FontSize.Medium">72</sys:Double>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="{StaticResource FontSize.Medium}"/>
            <Setter Property="Background" Value="Pink"/>
        </Style>
    </Grid.Resources>
    <Button Content="Text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
Is it possible that you have the Buttons inside a Viewbox, so no matter what font size you set, the Buttons always stretch to fill the Viewbox?
 
asusralis
Topic Author
Posts: 60
Joined: 30 Jul 2018, 05:03

Re: Unable to change button font size

19 Sep 2019, 00:06

There are no ViewBoxes in the project. It's odd because I don't remember ever being able to change a button text size in this project - created a few months ago - but creating a new project allows me to change button font sizes like normal.

This is all that is happening in a specific view I've tested:
<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Assets/Project/Views/Resources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Button Content="Test" FontSize="60"/>
    </Grid>
I have styles in my resources for scrollviewers, progressbars, listboxes, tooltips... But I don't see how they would affect any of this. I've shown you the only two styles in my resources that have to do with textblocks and buttons.
 
User avatar
sfernandez
Site Admin
Posts: 1785
Joined: 22 Dec 2011, 19:20

Re: Unable to change button font size

24 Sep 2019, 09:39

but creating a new project allows me to change button font sizes like normal.
Could you add to that new project (the one working) the resources from the old project (a few every time) to determine which one is causing this issue?
 
asusralis
Topic Author
Posts: 60
Joined: 30 Jul 2018, 05:03

Re: Unable to change button font size

09 Jan 2020, 12:48

It seems when I remove my TextBlock style I can set the button's FontSize, but when it's there I cannot.
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="{StaticResource FontSize.Normal}" />
        <Setter Property="FontFamily" Value="{StaticResource Font}" />
        <Setter Property="Foreground" Value="White" />
    </Style>
    
     <Style TargetType="Button">
        <Setter Property="FontSize" Value="{StaticResource FontSize.Normal}" />
        <Setter Property="FontFamily" Value="{StaticResource Font}" />
    </Style>
    ...
    
    <StackPanel>
            <StackPanel.Resources>
                <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
                    <Setter Property="FontSize" Value="{StaticResource FontSize.VeryLarge}" />
                </Style>
            </StackPanel.Resources>
            <Button Content="My Text"/>
        </StackPanel>
    
In this example, setting the FontSize in the derived implicit (?) style declared in the StackPanel or directly to the "My Text" button does not change its FontSize unless I remove the implicit style declared in my main Resources.
 
User avatar
sfernandez
Site Admin
Posts: 1785
Joined: 22 Dec 2011, 19:20

Re: Unable to change button font size

16 Jan 2020, 11:56

Would you mind creating a ticket in our bugtracker and attach your project (it can be a private ticket if you need to)?
Everything I test here works as expected and I'm not able to reproduce that problem.

Thanks for your collaboration.

Who is online

Users browsing this forum: Bing [Bot] and 7 guests