如何将样式应用于 WPF 中的窗口控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I apply a style to the Window Control in WPF?
提问by Andreas Grech
I am setting a style for the Window in the App.xaml
like such:
我正在为 Window 设置一个样式,App.xaml
如下所示:
<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.
我基本上希望每个 Window 都将其 WindowStyle 的属性值设置为 None(以删除默认的窗口框架和边框);但它不起作用。
What am I missing here?
我在这里缺少什么?
回答by Quintin Robinson
I believe you have to name the style and apply it to each window like the following..
我相信您必须命名样式并将其应用到每个窗口,如下所示..
In app.xaml/resources..
在 app.xaml/resources..
<Style x:Key="MyWindowStyle" TargetType="Window">
<Setter Property="WindowStyle" Value="None"></Setter>
</Style>
Then in the window.xaml..
然后在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.
这应该可以工作,但简单地在资源中为 Window 应用带有 TargetType 的样式不会强制 Window 使用该样式,尽管它似乎适用于其他元素。
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.
如果您提供 TargetType,则该类型的所有实例都将应用该样式。但是派生类型不会......似乎。<Style TargetType="{x:Type Window}"> 不适用于您的所有自定义派生/窗口。<Style TargetType="{x:Type local:MyWindow}"> 将仅适用于 MyWindow。所以选项是
使用您指定为要应用该样式的每个窗口的 Style 属性的键控样式。设计器将显示样式窗口。
From the Question: How to set default WPF Window Style in app.xaml?
来自问题:如何在 app.xaml 中设置默认的 WPF 窗口样式?
The person who answered the question had a interesting idea about inheriting from a base window that has the style applied.
回答问题的人有一个关于从应用了样式的基本窗口继承的有趣想法。
回答by Andrew Mikhailov
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.
这是在 C# 4.0 中对我来说很好用的代码。它只是复制资源字典中所有子类的样式。
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.
现在 App.xaml 中的样式应该适用于所有窗口。
p.s. Yeah, I know this is not the cleanest or fastest way but it works. :)
ps 是的,我知道这不是最干净或最快的方法,但它有效。:)