WPF, Font style for multiple controls
Asked Answered
C

2

6

ok, I might be missing something really easy, But I want to use the same Font family, Font Size, and color for multiple controls.

Is there a way to create one style for this and apply it different controls?

Sorry if this has been asked before. Thanks

Cablegram answered 28/7, 2011 at 0:4 Comment(0)
D
5

Are the controls all in the same container? For example, in the same Window or StackPanel? If so, you can make set those properties on the parent container and they'll apply to any children. For example:

<StackPanel TextBlock.FontFamily="Comic Sans"
            TextBlock.FontSize="14"
            TextBlock.Foreground="Purple">

    <TextBlock Text="Yeah, baby! I love me some Comic Sans!" />
    <Button Content="Me too!" />
</StackPanel>

If you want to standardize the font across your entire app, you can use an implict style in your App.xaml file, like this:

<Style TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Comic Sans" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Foreground" Value="Purple" />
</Style>
Doggoned answered 28/7, 2011 at 0:18 Comment(2)
Hey thanks a lot for the response....Would option 2 require me to do anything to Control Templates in order to obtain the font style, or would it simply apply that font style to all Text Blocks that exist on the page and in Control Templates?Cablegram
@Cablegram You shouldn't have to do anything to existing control templates unless they're specifically overriding the TextBlock style or properties. Otherwise it'll just apply.Doggoned
D
1

I wanted to add this for the newbies (like myself).

If you want to set a property for multiple items within a container:

You can set a "style" within the "resources" for a control like so:

<Grid>

    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="22"/>
        </Style>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0" Text="hello text" />
    <TextBlock Grid.Row="1" Text="hello text1" />
    <TextBlock Grid.Row="2" Text="hello text2" />

</Grid>
Dialogize answered 2/7, 2020 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.