0

I'm trying to add a global style (Font size and Font family) into my WPF application for Window I have, but no style Is applied to It, whatever I do. I think my problem Is that my startup Window Is not App.xaml, because I use App.xaml just to check If user has permission to run application. But right after that my desired Window opens, so StartupUri in my App.xaml Is set to that Window.

Here is my App.xaml:

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyApp"
             StartupUri="FirstWindowToShow.xaml">

    <Application.Resources>

        <!--Style that should be applied to all Windows-->
        <Style x:Key="Win_style" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Comic Sans MS" />
            <Setter Property="FontSize" Value="14" />
        </Style>

        <!--Style for all Pages - works fine-->
        <Style x:Key="PageFont" TargetType="{x:Type Page}">
            <Setter Property="FontFamily" Value="Comic Sans MS" />
            <Setter Property="FontSize" Value="12" />
        </Style>

    </Application.Resources>

</Application>

And here is my FirstWindowToShow.xaml :

   <Window x:Class="MyApp.FirstWindowToShow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Priprava_Podatkov"
        mc:Ignorable="d"
        Title="Some title" Height="480" Width="800" Loaded="Window_Loaded" Background="#FFF9F9F9" OpacityMask="Black">

    <Grid>

        <Menu x:Name="G_Menu" HorizontalAlignment="Left" VerticalAlignment="Top" Height="20" Width="792">
            <MenuItem x:Name="Menu_Program">
                <MenuItem x:Name="Menu_V" Header="About" Click="Menu_V_Click"/>
                <MenuItem x:Name="Menu_End" Header="Close" Click="Menu_End_Click"/>
            </MenuItem>
            <MenuItem Header="Department 1" Height="20" Width="148">
                <MenuItem x:Name="Dept_1" Header="Custom controlling" Click="Dept_1_Click"/>
            </MenuItem>
        </Menu>
        <Frame x:Name="Frame_s" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="772" NavigationUIVisibility="Hidden"/>

        <StatusBar DockPanel.Dock="Bottom" Margin="0,386,0,0" VerticalAlignment="Bottom" Background="Transparent">
            <StatusBarItem Width="73">
                <Label Content="User:" FontWeight="Bold" Width="73"/>
            </StatusBarItem>
            <StatusBarItem>
                <Label x:Name="LblU" Content="user" FontWeight="Light"/>
            </StatusBarItem>
            <StatusBarItem>
                <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Height="10" />
            </StatusBarItem>
            <StatusBarItem>
                <Label Content="User permissions:" FontWeight="Bold" />
            </StatusBarItem>
            <StatusBarItem>
                <Label x:Name="LblN" Content="Rights" FontWeight="Light"/>
            </StatusBarItem>
            <StatusBarItem >

                <Label x:Name="Lbl_P" Content="Data exported..." >
                    <Label.Style>
                        <Style TargetType="{x:Type Label}">
                            <Style.Resources>
                                <Storyboard x:Key="flashAnimacija">
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:1.5" RepeatBehavior="Forever" />
                                </Storyboard>
                            </Style.Resources>

                            <Setter Property="Visibility" Value="Hidden" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName= Progress_DoKonca, Path= IsVisible}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimacija}" />
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="flash"/>
                                    </DataTrigger.ExitActions>
                                </DataTrigger>

                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                </Label>
            </StatusBarItem>
            <StatusBarItem HorizontalAlignment="Right" Margin="-10,0,10,0">
                <Grid>
                    <ProgressBar x:Name="Progress_TillEnd" Width="150" Height="20" />
                    <TextBlock x:Name="Progress_Txt" Text="{Binding ElementName=Progress_DoKonca, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>

I have been trying all sorts of things in code or XAML, like this or this, but still with no success. What am I doing wrong ?

Lucy82
  • 654
  • 2
  • 12
  • 32
  • When you say 'no style is applied', do your existing controls have default font e.g. Arial? Or the control text is not visible? Can you remove the Foreground="{x:Null}" from the FirstWindowToShow and try? – Insane Dec 03 '19 at 11:43
  • @Insane, I posted full Window code since It's not that long. I have been trying to delete everything I came up with, but desired font just Isn't showing inside Window. I have tried **Foreground="{x:Null}"** too. – Lucy82 Dec 03 '19 at 11:56
  • Looks like StatusBar and Menu are causing me problems. Everything Is fine If I set their styles manually. Strangely style for Pages work, but I have no StatusBar or Menu there, – Lucy82 Dec 03 '19 at 12:38
  • Each window you have is not typed as a window. It is a MainWindow or a Window1 or whatever. Hence your style will target none. Simplest approach is to give the style a key and explicitly reference it in each window. – Andy Dec 03 '19 at 12:53
  • @Andy, how do you mean that ? My FirstWindowToShow inhertis from Wind class. I tried even ** – Lucy82 Dec 03 '19 at 12:58
  • @Lucy82, Try to reference to properties ot the parent window like: ` – Jackdaw Dec 03 '19 at 13:16
  • I mean give it a key foo and use style=dynamicresource foo in the window. You will need your style to use basedon to reference the base style for a window. Having said that. You only have 2 setters. If that's all you're using then just paste the two lines into your windows. – Andy Dec 03 '19 at 13:19
  • Or you could use the approach in answer here https://stackoverflow.com/questions/3145511/how-to-set-the-default-font-for-a-wpf-application – Andy Dec 03 '19 at 13:40

3 Answers3

3

This is what I've done in the past, so see if it works for you:

In your App.xaml, remove the x:Key from the Window style, so it becomes:

    <!--Style that should be applied to all Windows-->
    <Style TargetType="{x:Type Window}">
        <Setter Property="FontFamily" Value="Comic Sans MS" />
        <Setter Property="FontSize" Value="14" />
    </Style>

Then in your App.xaml.cs (code-behind), override the OnStartup method and add this code:-

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
        {
            DefaultValue = FindResource(typeof(Window))
        });
    }

This will apply those styles in the App.xaml Window style (i.e. FontFamily and FontStyle) to all windows created by the application.

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • that doesn't work for me either. I think I will need to recreate project, something Is wrong. Could maybe a cause of all problem be a fact that I created a folder "WINDOW" and put both files there - App.xaml & FirstWindowToShow.xaml, Is WPF very sensitive about moving files ? – Lucy82 Dec 03 '19 at 13:40
1

For the controls like Menu and StatusBar it is necessary to set the style explicitly like below:

<Style  x:Key="BaseStyle" TargetType="{x:Type Control}">
   <Setter Property="FontFamily" Value="Comic Sans MS" />
   <Setter Property="FontSize" Value="13" />   
</Style>

<Style TargetType="{x:Type StatusBar}" BasedOn="{StaticResource BaseStyle}" />        
<Style TargetType="{x:Type Menu}" BasedOn="{StaticResource BaseStyle}" />        
<Style TargetType="{x:Type local:Window1}" BasedOn="{StaticResource BaseStyle}" />
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • that is what I figured out too. But guys say that their solution work too. So I have to recreate project to confirm that. – Lucy82 Dec 03 '19 at 14:10
  • @Lucy, I tried create project from scratch but this does not worked for me. I hope will work for you! – Jackdaw Dec 03 '19 at 14:11
  • you mean solution from Andrew and mm8 ? mm8 stated that It worked for him. – Lucy82 Dec 03 '19 at 14:27
  • I think you are right. I have come to same conclusion before you but I marked your answer, thanks for all effort. Maybe there Is a way to do this even with these controls, but I'm sure It's not easy :) – Lucy82 Dec 03 '19 at 18:26
  • @Lucy82, I'm glad this solution works for you. There are some additional comments in the following post: [https://stackoverflow.com/questions/59162456/the-ability-to-propagate-default-values-down-the-element-tree-doesnt-work](https://stackoverflow.com/questions/59162456/the-ability-to-propagate-default-values-down-the-element-tree-doesnt-work). – Jackdaw Dec 03 '19 at 23:12
0

Why would a Style with an x:Key of "Win_style" be applied to all windows?

You could keep Win_style and define an implict Style per window type (e.g. FirstWindowToShow) that is based on Win_style:

<Application.Resources>

    <!--Style that should be applied to all Windows-->
    <Style x:Key="Win_style" TargetType="{x:Type Window}">
        <Setter Property="FontFamily" Value="Comic Sans MS" />
        <Setter Property="FontSize" Value="14" />
    </Style>

    <!-- implicit window styles, one for each window -->
    <Style TargetType="{x:Type local:FirstWindowToShow}" BasedOn="{StaticResource Win_style}" />

    <!--Style for all Pages - works fine-->
    <Style x:Key="PageFont" TargetType="{x:Type Page}">
        <Setter Property="FontFamily" Value="Comic Sans MS" />
        <Setter Property="FontSize" Value="12" />
    </Style>

</Application.Resources>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes, it does. Create a new WPF application using the defailt template, copy the sample markup from answer into `App.xaml`and replace `FirstWindowToShow` with `MainWindow` and you'll see that it works. – mm8 Dec 03 '19 at 13:41
  • yes that will be my next step, looks like something is wrong with my project, no solutions work for me. Must have been something that I did in past. – Lucy82 Dec 03 '19 at 13:45
  • confirmed that It doesn't work even in new WPF project, you didn't try It on xaml markup that I posted. Correct answer Is what Jackdaw posted. – Lucy82 Dec 03 '19 at 18:24
  • @Lucy82: Your question doesn't mention anything about some `Menu` and `StatusBar`, does it? This does certainly work to solve the issue of applying a style to a window. – mm8 Dec 04 '19 at 12:28
  • @Yes It doesn't, because we didn't know that those controls were causing a problem. However I posted a whole xaml of Window, and that Is how we figured It out. – Lucy82 Dec 04 '19 at 12:38