Search code examples
wpfcontrolsstylesfont-family

WPF, Font style for multiple controls


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


Solution

  • 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>