如何在 app.xaml 中设置默认 WPF 窗口样式? [英] How to set default WPF Window Style in app.xaml?
问题描述
我试图在我的 app.xaml 中为我的 WPF Windows 应用程序中的每个窗口设置默认样式.到目前为止,我在 app.xaml 中有这个:
I am trying to set the default Style for every window in my WPF Windows application in my app.xaml. So far i have this in app.xaml:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="Background" Value="Blue" />
</Style>
</ResourceDictionary>
</Application.Resources>
在运行应用程序时(但不是在 VS 设计器中),我可以通过以下方式明确告诉窗口使用此样式,从而使窗口以这种样式显示:
I can get the window to appear with this style when running the app (but not in VS designer) by specifically telling the window to use this style via:
Style="{DynamicResource WindowStyle}
这可行,但并不理想.那我该怎么办:
This works, but is not ideal. So how do I:
- 是否让所有窗口自动使用该样式(所以我不必在每个窗口上都指定它)?
- VS 设计师是否展示了风格?
谢谢!
推荐答案
补充一下 Ray 所说的:
To add on to what Ray says:
对于样式,您需要提供密钥/ID 或指定目标类型.
For the Styles, you either need to supply a Key/ID or specify a TargetType.
如果 FrameworkElement 没有明确指定的样式,它将总是寻找风格资源,使用自己的类型作为键
- 编程 WPF (Sells, Griffith)
If a FrameworkElement does not have an explicitly specified Style, it will always look for a Style resource, using its own type as the key
- Programming WPF (Sells, Griffith)
如果您提供 TargetType,则该类型的所有实例都将应用该样式.但是派生类型不会......似乎.<Style TargetType="{x:Type Window}">
不适用于您的所有自定义派生/窗口. 将仅适用于 MyWindow.所以选项是
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.
.
<Application.Resources>
<Style x:Key="MyWindowStyle">
<Setter Property="Control.Background" Value="PaleGreen"/>
<Setter Property="Window.Title" Value="Styled Window"/>
</Style>
</Application.Resources> ...
<Window x:Class="MyNS.MyWindow" Style="{StaticResource MyWindowStyleKey}"> ...
- 或者您可以从自定义 BaseWindow 类派生(该类具有 它自己的怪癖),您可以在 Ctor/Initialization/Load 阶段设置 Style 属性一次.然后所有派生都将自动应用该样式.但设计师不会注意到您的风格您需要运行您的应用程序才能看到所应用的风格..我猜设计师只是运行 InitializeComponent (这是自动/设计师生成的代码)因此应用了 XAML,但不应用自定义代码隐藏.
- Or you could derive from a custom BaseWindow class (which has its own quirks), where you set the Style property during the Ctor/Initialization/Load stage once. All Derivations would then automatically have the style applied. But the designer won't take notice of your style You need to run your app to see the style being applied.. I'm guessing the designer just runs InitializeComponent (which is auto/designer generated code) so XAML is applied but not custom code-behind.
所以我会说明确指定的样式是最少的工作.无论如何,您都可以集中更改样式的各个方面.
So I'd say explicitly specified styles are the least work. You can anyways change aspects of the Style centrally.
这篇关于如何在 app.xaml 中设置默认 WPF 窗口样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!