Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply a style to the Window Control in WPF?

Tags:

styles

wpf

xaml

I am setting a style for the Window in the App.xaml like such:

<Application x:Class="MusicRepo_Importer.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" StartupUri="TestMaster.xaml">
    <Application.Resources>

        <Style TargetType="Window">
            <Setter Property="WindowStyle" Value="None"></Setter>
        </Style>

    </Application.Resources>
</Application>

With which I basically want every Window to have its WindowStyle's property value set to None (to remove the default windows frame and border); But it is not working.

What am I missing here?

like image 426
Andreas Grech Avatar asked Mar 26 '09 01:03

Andreas Grech


2 Answers

I believe you have to name the style and apply it to each window like the following..

In app.xaml/resources..

<Style x:Key="MyWindowStyle" TargetType="Window">
    <Setter Property="WindowStyle" Value="None"></Setter>
</Style>

Then in the window.xaml..

<Window x:Class="MusicRepo_Importer.MyWindow"
    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"
    Title="MyStyledWindow" Style="{StaticResource MyWindowStyle}">

This should work, but simply applying the style with TargetType for Window in the resource won't force the Window to use that style although it seems to work for other elements.

Edit:
Found some info in relation to applying default styles to a window element..

If you supply a TargetType, all instances of that type will have the style applied. However derived types will not... it seems. <Style TargetType="{x:Type Window}"> will not work for all your custom derivations/windows. <Style TargetType="{x:Type local:MyWindow}"> will apply to only MyWindow. So the options are

Use a Keyed Style that you specify as the Style property of every window you want to apply the style. The designer will show the styled window.

From the Question: How to set default WPF Window Style in app.xaml?

The person who answered the question had a interesting idea about inheriting from a base window that has the style applied.

like image 110
Quintin Robinson Avatar answered Nov 02 '22 15:11

Quintin Robinson


I know this question is quite old but I will answer anyway.

Here is the code that works fine for me in C# 4.0. It just duplicates style for all subclasses in the resource dictionary.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (this.Resources.Contains(typeof(Window)))
        {
            var types = Assembly.GetEntryAssembly().GetTypes();
            var subTypes = types.Where(x => x.IsSubclassOf(typeof(Window)));

            Style elementStyle = (Style)this.Resources[typeof(Window)];

            foreach (Type subType in subTypes)
            {
                if (!this.Resources.Contains(subType))
                {
                    this.Resources.Add(subType, elementStyle);
                }
            }
        }

        base.OnStartup(e);
    }
}

Now your style from App.xaml should work for all windows.

p.s. Yeah, I know this is not the cleanest or fastest way but it works. :)

like image 40
Andrew Mikhailov Avatar answered Nov 02 '22 13:11

Andrew Mikhailov