Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Change fontSize of button with style fails?

Tags:

styles

wpf

Well i have my file Styles.xaml thats merged in the Application.xaml so it applies to every thing..

here are my styles

<Style TargetType="{x:Type Control}" x:Key="baseStyle">
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="12"/>
</Style>

<Style TargetType="Button" BasedOn="{StaticResource baseStyle}">
    <Setter Property="Margin" Value="2,0,2,0"/>
    <Setter Property="Padding" Value="2"/>
    <Setter Property="FontSize" Value="50"/>
</Style>

<Style TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="12"/>
</Style>

When im in the editor this seems to work but when i run the application the font-size of the buttons are shrinked to their normal sizes..

My guess is that the buttons create a TextBlock when their content is set to a string and then use the textblock style.. but how can i override this?

like image 982
Peter Avatar asked Nov 26 '10 10:11

Peter


1 Answers

You're right about

My guess is that the buttons create a TextBlock when their content is set to a string and then use the textblock style

. See this post.

A workaround is to define a DataTemplate for System.String, where we can explicitly use a default TextBlock to display the content. You can place that DataTemplate in the same dictionary you define the TextBlock style so that this DataTemplate will be applied to whatever ContentPresenter effected by your style.

So adding the DataTemplate at the end to Styles.xaml will fix the problem

<ResourceDictionary 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">
    <Style TargetType="{x:Type Control}" x:Key="baseStyle">
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}">
        <Setter Property="Margin" Value="2,0,2,0"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="FontSize" Value="50"/>
    </Style>

    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="Foreground" Value="Green" />
        <Setter Property="FontSize" Value="24"/>
    </Style>

    <DataTemplate DataType="{x:Type sys:String}">
        <TextBlock Text="{Binding}">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}"/>
            </TextBlock.Resources>
        </TextBlock>
    </DataTemplate>
</ResourceDictionary>

This will keep your Style for a TextBlock but the TextBlock created in a Button for example won't be effected by it

like image 58
Fredrik Hedblad Avatar answered Nov 15 '22 09:11

Fredrik Hedblad