How can I apply style to Window Control in WPF?

I set the window style in App.xaml , for example:

 <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 what I basically want every window to have the WindowStyle property set to None (to remove the window frame and default border); But it does not work.

What am I missing here?

+7
styles wpf xaml
source share
2 answers

I believe that you should name the style and apply it to each window, as shown below.

In app.xaml / resources ..

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

Then in the .xaml .. window

 <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 just using the style with TargetType for Window in the resource will not force Window to use this style, although it seems to work for other elements as well.

Edit:
Found some information regarding applying default styles to a window element.

If you specify TargetType, all instances of this type will have the style applied. However, derived types will not ... seem. <TargetType Style = "{x: Type Window}"> will not work with all of its Differentiation / Window usages. <TargetType style = "{x: Type local: MyWindow}"> will only apply to MyWindow. So the parameters

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

From the question: How to set the default WPF Window style in app.xaml?

The person who answered the question had an interesting idea of ​​inheritance from the base window in which the style was applied.

+19
source share

I know this question is pretty old, but I will answer anyway.

Here is the code that works great for me in C # 4.0. It simply duplicates the 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.

ps Yes, I know that this is not the cleanest or fastest way, but it works. :)

+7
source share

All Articles