0

I have a class named MyWindow that derives from Window:

using System.Windows;

public class MyWindow : Window
{
}

And I use that class in MainWindow.xaml:

<MyWpfApp:MyWindow x:Class="MyWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication1="clr-namespace:WpfApplication1"
          Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Text="Test" Foreground="Orange"/>
    </Grid>
</MyWpfApp:MyWindow>

And in the App.xaml file, I add the following style to override the Background property of all MyWindow instances:

<Application.Resources>
    <Style TargetType="MyWpfApp:MyWindow">
        <Setter Property="Background" Value="Black"></Setter>
    </Style>
</Application.Resources>

But this doesn't change anything at all. No style is applied to MainWindow.

What should I do so that I can use a global style for all MyWindows?

P.S.: I checked out "Modern UI"s code, I saw that they apply something like the following in their window's constructor:

using System.Windows;

public class MyWindow : Window
{
    public MyWindow()
    {
        DefaultSyleKey = typeof (MyWindow);
    }
}

But if I do this, MainWindow ends up being completely black in the content area. I am guessing that somehow its Template property gets overriden, but I don't understand how, and how to make this work.

hattenn
  • 4,371
  • 9
  • 41
  • 80
  • [This SO answer here](http://stackoverflow.com/a/1232333/1751090) provides a good explanation of applying default themes used above. – Rachael Aug 20 '13 at 21:39

1 Answers1

1

Ok I have worked it out. Apparently there's a special file you can use named generic.xaml.

You need to add your style definitions there and then add that file to a directory called Themes.

WPF will end up using that as a fall back: Themes\generic.xaml

hattenn
  • 4,371
  • 9
  • 41
  • 80